ways to check if a variable is undefined in javascript

var x;

1. x === undefined  (returns true)

2. typeof x == ‘undefined’ (returns true)

3. !x (returns true)

4. x == void 0 (returns true, not a good idea though because it is hard to understand)

4. if you have a variable x, when its value is undefined but typeof x returns ‘string’ and x === undefined returns false, then try x == ‘undefined’. 🙂

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