javascript

Null, Undefined and “Not defined”

Null:

Null is the intentional absence of any object value. This could be a property of an object, or an expected value of a variable. It is denoted by a literal “null’ and it expresses a total lack of identity.

Example

let person = {
"name": "Lawson Luke",
"hobby": null
}
console.log(person.hobby); //null

When validating user inputs, values can also be checked for null values

let email = null;
if(email === null){
alert('Please enter your email');
}

Undefined

Undefined, in the simplest term, means that a property of variable is not initialized or does not exist.

let person = {
"name": "Lawson Luke",
"hobby": "coding",
}
console.log(person.age); //undefined

Not defined

“Not defined” refers to a variable that is not declared at any given point in time with the declaration keyword like var, let or const.

console.log(name); //Uncaught ReferenceError: name is not defined

Conclusion

Now we know the difference between null, undefined and “not defined”, and we can begin to write cleaner code and better logic.

Leave a Reply

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