What's the best way of checking if an object property in JavaScript is undefined?
Sorry, I initially said variable rather than object property. I believe the same == undefined approach doesn't work there.
What's the best way of checking if an object property in JavaScript is undefined?
Sorry, I initially said variable rather than object property. I believe the same == undefined approach doesn't work there.
if (somevariable == undefined) {
alert('the variable is not defined!');
}
You can also make it into a function, as shown here:
function isset(varname){
return(typeof(window[varname]) != 'undefined');
}
In JavaScript there is null and there is undefined. They have different meanings.
Marijn Haverbeke states, in his free, online book "Eloquent JavaScript" (emphasis mine):
There is also a similar value, null, whose meaning is 'this value is defined, but it does not have a value'. The difference in meaning between undefined and null is mostly academic, and usually not very interesting. In practical programs, it is often necessary to check whether something 'has a value'. In these cases, the expression something == undefined may be used, because, even though they are not exactly the same value, null == undefined will produce true.
So, I guess the best way to check if something was undefined would be:
if (something == undefined)
Hope this helps!
Edit: In response to your edit, object properties should work the same way.
var person = {
name: "John",
age: 28,
sex: "male"
};
alert(person.name); // "John"
alert(person.fakeVariable); // undefined
It's better to use the strict equality operator:
if (variable === undefined) {
alert('not defined');
}
x == undefined also checks whether x is null, while strict equality does not (if that matters).(source)
Or you can simply do this:
if (!variable) {
alert('not defined');
}
Here you check if there's any value that can make the variable look false (undefined, null, 0, false, ...). Not a good method for integers ('0' is not false), but might do well for object properties.
The solution is incorrect. In javascript,
null == undefined
will return true because they both are "casted" to a boolean and are false. The correct way would be to check
if (something === undefined)
which is the identity operator...
Use:
if (typeof something == "undefined")
alert("something is undefined");
I've tested with undefined but is not recognized on object as standard command (i probably wrong same case sensitive), as said Ricky undefined = null. I've tested with null and finally work:
document.getElementById("objectname") === null
I've used it to check(uncheck) unknown number of checkboxes without test all objects in the page.
function isUnset(inp) {
return !!(typeof inp === 'undefined')
}
return false if variable is set, and true if is undefined.
use::
if (isUnset(var)){
//Handle initialization
}
if ( typeof( something ) == "undefined")
This worked for me while the others didn't.
I believe there are a number of incorrect answers to this topic. Contrary to common belief "undefined" is NOT a keyword in javascript, and can in fact have a value assigned to it.
var undefined = false;
if(myVar === undefined) { // <-- undefined should not be blue text
alert("it was undefined");
}
Additionally, "myVar === undefined" will raise an error in the situation where myVar is undeclared.
The most robust way to perform this test is:
if(typeof myVar === "undefined")
This will always return the correct result, and even handles the situation where myVar is not declared.
Please try the following example it works for me and it is as per w3schools standard
var t1;
if (t1 == undefined) {
alert('not defined');
}
I'm not sure where the origin of using === with typeof came from, and as a convention I see it used in many libraries, but the typeof operator returns a string literal, and we know that up front, so why would you also want to type check it too?
typeof x; // some string literal "string", "object", "undefined" if (typeof x === "string") { // === is redundant because we already know typeof returns a string literal if (typeof x == "string") { // sufficient