Advanced Javascript Tutorial

Infinity, NaN, undefined, null

Infinity

Represent infinity value, in most cases dividing with zero.

var inf=5/0;

var type=typeof inf;

console.log(type +': '+ inf); //number: Infinity

 

Infinity example: 20var_infinity.js

 

 

Nan

When you try to apply Number method on Not a Number.

var str='some text'; //string
var numb=parseInt(str); //or parseFloat()

var type=typeof numb;

console.log(type +': '+ numb); //number: NaN

NaN example: 21var_NaN.js

 

 

undefined

 If the variable is not defined.

There are certain difference between browser and Node. Node returns undefined but browser returns string.


var type=typeof name;

//browser - string:
//nodeJS - undefined:
console.log(type +': '+ name);

undefined example: 22var_undefined2.js

another example where variable is defined but dont have value: 22var_undefined.js

 

 

 

 null

Variable is defined and exists but don't have any value assigned.

there is a difference between

var x = ' ';  //empty string

var x = null;  //empty object

 

x = 23;
x = null; //redefine variable x
var type=typeof x;

console.log(type +': '+ x); //string:


if(x==null) console.log('x is null'); //works
else if(x=='') console.log('x is empty string'); //dont work

 

null object example: 23var_null_obj.js

empty string: 23var_empty_string.js