Sunday, 9 December 2018

What values are evaluated to false in JavaScript?

Below values are evaluated to false in JavaScript.
a.   false
b.   Undefined
c.   null
d.   0
e.   NaN
f.    the empty string ("")

All the other values are evaluated to true.

HelloWorld.js
var a = false;
var b;
var c = null;
var d = 0;
var e = NaN;
var f = "";

if(a){
  console.log(`${a} is evaluated to true`);
}else{
  console.log(`${a} is evaluated to false`);  
}

if(b){
  console.log(`${b} is evaluated to true`);
}else{
  console.log(`${b} is evaluated to false`);  
}

if(c){
  console.log(`${c} is evaluated to true`);
}else{
  console.log(`${c} is evaluated to false`);  
}

if(d){
  console.log(`${d} is evaluated to true`);
}else{
  console.log(`${d} is evaluated to false`);  
}

if(e){
  console.log(`${e} is evaluated to true`);
}else{
  console.log(`${e} is evaluated to false`);  
}

if(f){
  console.log(`${f} is evaluated to true`);
}else{
  console.log(`${f} is evaluated to false`);  
}

Output
false is evaluated to false
undefined is evaluated to false
null is evaluated to false
0 is evaluated to false
NaN is evaluated to false
 is evaluated to false

Previous                                                 Next                                                 Home

No comments:

Post a Comment