Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test if a variable is defined in javascript? [duplicate]

Tags:

javascript

How should I test if a variable is defined?

if //variable is defined     //do this else     //do this 
like image 898
boom Avatar asked Sep 28 '11 05:09

boom


People also ask

How do you check if a variable is defined or not in JavaScript?

Use the typeof operator to check if a variable is defined or initialized, e.g. if (typeof a !== 'undefined') {} . If the the typeof operator doesn't return a string of "undefined" , then the variable is defined.

What does __ do in JavaScript?

The __filename in the Node. js returns the filename of the code that is executed. It gives the absolute path of the code file.

How do you check if a variable is equal to a value?

Use the strict equality (===) operator to check if a variable is equal to true - myVar === true . The strict equality operator will return true if the variable is equal to true , otherwise it will return false .


1 Answers

if (typeof variable !== 'undefined') {   // .. } else {      // .. } 

find more explanation here:

JavaScript isset() equivalent

like image 152
Mithun Satheesh Avatar answered Sep 21 '22 19:09

Mithun Satheesh