Some Java script nuances – null, undefined, NaN

Undefined

  • Javascript has a variable whose value is undefined and typeof undefined is also undefined
  • Undefined is not a type or constant
  • Undefined is a type with exactly one value: undefined
  • You can get Undefined in various different ways A declared variable without assigning any value to it. Implicit returns of functions due to missing return statements. Return statements that do not explicitly return anything. Lookups of non-existent properties in an object. Function parameters that have not passed. Anything that has been set to the value of undefined

null

  • It means empty or non existent value, it is also used to indicate no value
  • It is primitive type and u can use null to assign to any variable. null is not an object it is primitive value
  • typeof null returns object. But null is not an object, you cannot add properties to it.

null == undefined – This returns true
null === undefined – This returns false

NaN

  • The NaN property represents a value that is ‘not a number’. This special value results from an operation that could not be performed either because one of the operands was non-numeric (e.g., ‘abc’ / 4), or because the result of the operation is non-numeric (e.g., an attempt to divide by zero). While this seems straightforward enough, there are a couple of somewhat surprising characteristics of NaN that can result in hair-pulling bugs if one is not aware of them.
  • For one thing, although NaN means ‘not a number”, its type is, believe it or not, Number: console.log(typeof NaN === ‘number’); // logs ‘true’
  • NaN compared to anything – even itself! – is false: console.log(NaN === NaN); // logs ‘false’
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s