Saturday, 8 December 2018

How the null behaves in boolean and numeric context?

'null' behaves as false in boolean context.

HelloWorld.js
var x = null;

if(x){
  console.log("null is evaluated to true in boolean context");  
}else{
  console.log("null is evaluated to false in boolean context");
}


Output

null is evaluated to false in boolean context

'null' is evaluated to 0 in numeric context.

HelloWorld.js
var x = null;

var y = x + 10;

console.log("Value of y is : " + y);


Output

Value of y is : 10





Previous                                                 Next                                                 Home

No comments:

Post a Comment