开发者

Conditional Statement in JavaScript

开发者 https://www.devze.com 2023-03-16 08:37 出处:网络
In C#.Net, We would use Conditional Statement like this below: string Place = TextBox1.Text == \"\" ? \"School\" : TextBox1.Text;

In C#.Net, We would use Conditional Statement like this below:

string Place = TextBox1.Text == "" ? "School" : TextBox1.Text;

How to use Conditional Statement in JavaScript. I am assigning one value to the TextBox, If there is no value then I want to assign "1" to the TextBox.

Here I used like this,

document.getElementById('<%=txtPlace.ClientID %>').value   = obj[1];

If obj[1] == "" then I want to assign "1" to the TextBox. How to assign? It ca开发者_如何转开发n be done easily by using If statement. But I want to know how to use Conditional Statement in JavaScript? Is there Conditional Statement in JavaScript? If so then how to use it?


Yes, Javascript does support the conditional operator:

document.getElementById('<%=txtPlace.ClientID %>').value = obj[1] ? obj[1] : "1";

Alternatively, you can take advantage of its short-circuiting logical OR operator:

document.getElementById('<%=txtPlace.ClientID %>').value = obj[1] || "1";


Yes, there is conditional statement in javascript, it works the same way:

document.getElementById('<%=txtPlace.ClientID %>').value = obj[1] === "" ? "1" : obj[1];


The conditional (or ternary) operator is the same in JavaScript:

condition ? true-value : false-value

So your code would look like this:

document.getElementById('<%=txtPlace.ClientID %>').value = obj[1] === "" ? "1" : obj[1];


Yes, there is and it behaves in the same way as in C#.

document.getElementById('<%=txtPlace.ClientID %>').value = obj[1]==""?"1":"something";
0

精彩评论

暂无评论...
验证码 换一张
取 消