6

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.

5 Answers 5

10

Use the typeof operator:

if(typeof objCR == "undefined")
   alert("objCR is not defined");
Sign up to request clarification or add additional context in comments.

4 Comments

..yes, beat me to it as well!
This doesn't fully answer the question as I understand it because objCR could easily be declared within the current scope but still be undefined. For example, var objCR; would do that.
This will detect if the variable exists in any scope. It doesn't it to just the current function scope. I don't think there's any way to limit that variable to just the current scope.
Thank you for this recipe. One correction: I prefer the strict equality operator, '===' (or '!==' in my own use case).
3

As mentioned by others, using a typeof check will get you some of the way there:

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

However, this won't distinguish between objCR existing and being undefined (as would be the case if it had been declared but not assigned to, e.g. using var objCR;) and objCR never having been declared anywhere in the scope chain, which I think is what you actually want. If you want to be sure that no objCR variable has even been declared, you could use try/catch as follows:

try {
    objCR; // ReferenceError is thrown if objCR is undeclared
} catch (ex) {
    alert("objCR has not been declared");
}

Comments

1
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

Comments

0

I would suggest the obvious:

if (objCR==undefined) ...

4 Comments

undefined is a JavaScript keyword ("The keyword 'undefined' indicates that a variable has no value or does not exist. This is different from null, but they are generally considered equal")
better to use typeof, as javascript doesn't protect the undefined keyword - you can actually have code which sets undefined = 42(!) which would break your comparison
(objCR==undefined) fails in IE6 at least if objCR is undefined. Use typeof to be safe.
The problems with this are: first, that you will get a ReferenceError in all browsers if objCR has never been declared; second, a comparison with undefined using == will also return true if objCR is null; third, that a variable that has been declared but not assigned a value will be undefined, and fourth, that undefined is not a reserved word and its value can be altered.
0

I always have this to be safe:

if(typeof objCR == "undefined" || objCR == null)
   alert("object is not defined or null");

2 Comments

null is definitely different from undefined, so this as it stands is wrong.
@Andrzej sorry I don't understand why it's wrong.. I'm double checking that's all. Isn't it possible for something to be defined but null? If you mean the message, it's now fixed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.