How to check variable / object is set or not in javascript?

Before few days we have a requirement to check that, the variable which we want to use is already set and defined or not.

To fulfill our requirement we have used javascript typeof operator.

The typeof operator in javascript returns the data type of given argument, such as whether an argument is numeric, string, boolean, object, null or undefined.

Here if we pass any variable / object as an argument which has not defined, then it returns undefined.

Following the example to check variable / object is set or not and its data type.

var check_numeric = 10;

alert(typeof(check_numeric));

alert(typeof("Hello"));

alert(typeof(true));

alert(typeof(check_undefined));

Speak Your Mind

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: