JavaScript Check If Variable Is Undefined

JavaScript Check If Variable Is Undefined | Since a null variable can pass a check for undefined in JavaScript if it is not expressed properly, determining if a variable is undefined can be a little tricky. Because of this, defined values can pass through and vice versa. Make careful to check if a value is equivalent to undefined using rigorous equality ===.

JavaScript Check If Variable Is Undefined Example

let x;
const y = null;

console.log(x == null && y == undefined); // true
console.log(x === null || y === undefined); // false

console.log(x == null && typeof x == 'object'); // false
console.log(x === undefined && typeof x == 'undefined'); // true

Output:-

true
false
false
true

Checking to see whether typeof x === “undefined” is another option. The main distinction between these two methods is that typeof does not raise a ReferenceError if x has not yet been declared, whereas x === undefined does.

JavaScript determines if x is a declared variable that is absolutely equal to undefined when it uses the expression x === undefined. We can use typeof x === ‘undefined’ to determine whether x is precisely equal to undefined irrespective of whether it has been declared or not.

 x === undefined; // Throws a ReferenceError
 typeof x == 'undefined'; // true

Identifying Undefined Object Properties

The ability to access a property that does not exist in the object and have JavaScript report its value as undefined rather than raising a ReferenceError makes it tricky to determine whether an object property is undefined.

const obj = { answer: 2, question: undefined };

console.log(obj.answer); // 2
console.log(obj.question); // undefined
console.log(obj.notInObject); // undefined

Output:-

2
undefined
undefined

In JavaScript, the expression obj.propName === undefined is valid if any obj has a property named “propName” and whose value is strictly equivalent to undefined, or obj doesn’t. Use the in operator to determine whether an object has a property if the property is strictly equal to undefined.

const obj = { answer: 20, question: undefined };

console.log('question' in obj && obj.question === undefined); // true
console.log('notInObject' in obj && obj.notInObj === undefined); // false

Output:-

true
false

If a variable is declared in JavaScript but not given a value, it is automatically given the value undefined. Therefore, the word “undefined” will be shown if you attempt to display the value of such a variable. The null, on the other hand, is a special assignment value that may be used to represent no value by assigning it to a variable.

In plain English, a null value denotes the absence of a value, while an undefined variable is one that has been declared but has not yet been given a value.

You can use the equality operator == or strict equality operator === to determine whether a variable is undefined or null (also called the identity operator).

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *