Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a variable is in the scope or not in Javascript

Tags:

javascript

I need to check if a object "objCR" is present in the current scope or not. I tried using below code.

if(objCR == null)
alert("object is not defined");

Let me know where I am wrong.

like image 682
Vaibhav Jain Avatar asked Dec 12 '22 17:12

Vaibhav Jain


2 Answers

Use the typeof operator:

if(typeof objCR == "undefined")
   alert("objCR is not defined");
like image 88
Oded Avatar answered Feb 11 '23 23:02

Oded


if (typeof objCR=="undefined"){
    alert("objCR is undefined");
} else {
    alert("objCR is defined");
};

(!objCR) will return true if objCR is a boolean equal to false

like image 45
El Ronnoco Avatar answered Feb 11 '23 23:02

El Ronnoco