javascript undefined, null, undefined property, 0, false, ”, ==, ===, typeof

== Equals

If the two operands are not of the same type, JavaScript converts the operands then applies strict comparison.

=== Strict equal

Returns true if the operands are strictly equal  with no type conversion.

typeof

The typeof operator returns a string indicating the type of the unevaluated operand.

typeof undefined === ‘undefined’

undefined

A variable that has not been assigned a value is of type undefined.

false

Examples of expressions that can be converted to false are those that evaluate to null, 0, the empty string (“”), or undefined.

null

null is an object. It’s type is null. Null and Undefined types are == (but not ===)

Undeclared variable

  1. /* var z*/
  2. z.x  // throws a ReferenceError

Check for undefined variable

  1. var x;
  2. if (x === undefined) {
  3.    // these statements execute
  4. }
  5. else {
  6.    // these statements do not execute
  7. }

check for undeclared variable

  1. // x has not been defined before
  2. if (typeof x === ‘undefined’) { // evaluates to true without errors
  3.    // these statements execute
  4. }
  5. if(x === undefined){ // throws a ReferenceError
  6. }

check for object property

  1. var myobj = {‘a’:’a’};
  2. myobj.hasOwnProperty(‘a’) // true
  3. var myobj2 = Object.create(myobj);
  4. myobj2.hasOwnProperty(‘a’) //false
  5. ‘a’ in myobj2 // true

check for null

  1. var c = null
  2. if (c === null) { // true
  3. }

References

http://saladwithsteve.com/2008/02/javascript-undefined-vs-null.html

http://constc.blogspot.com/2008/07/undeclared-undefined-null-in-javascript.html

http://www.mapbender.org/JavaScript_pitfalls:_null,_false,_undefined,_NaN

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/undefined

https://developer.mozilla.org/en/JavaScript/Reference/Operators/Comparison_Operators

https://developer.mozilla.org/en/JavaScript/Reference/Operators/typeof

http://www.nczonline.net/blog/2010/07/27/determining-if-an-object-property-exists/

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

%d bloggers like this: