sam sam -4 years ago 93
HTML Question

Can I declare optional values in a variable



var x = aa || 3 || dd;
document.getElementById("demo").innerHTML = x;

<h1>JavaScript Variables</h1>


<p id="demo"></p>





I wish to create variable with optional values

var a = x || b || 2


like if x is not undefined a should print value of x and so on

Answer Source

aa is not defined in your question

var aa = 0; (or) var aa = null (or) var aa = ''; will print 3

var aa = 0;
var x = aa || 3 || dd;
document.getElementById("demo").innerHTML = x;
<h1>JavaScript Variables</h1>


<p id="demo"></p>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download