Friday 7 December 2018

How to check a variable has a value or not?

By using ‘undefined’, you can check whether a variable has a value or not.

HelloWorld.js
function do_something() {
  var x = 100;
  
  if(x === undefined){
    console.log("x is undefined");
  }else{
    console.log("Value of x is : " + x);
  }
  
  var y;
  
  if(y === undefined){
    console.log("y is undefined");
  }else{
    console.log("Value of y is : " + y);
  }
}

do_something();

Output
Value of x is : 100
y is undefined



Previous                                                 Next                                                 Home

No comments:

Post a Comment