JavaScript Empty Values
What is an empty value?
Well, empty values are those values that are used to denote the absence of a meaningful value. They are themselves values, but without any information.
JavaScript has to empty values, null and undefined. Let’s explain each of them:
Null
Null is a type of value to be used for new variables with an explicit value of null. But null is also a type of object, but this is a JavaScript bug, Null should be null. Let’s see some examples.
let emptyValue = null; //Binding
console.log(emptyValue) // output -> nulltypeof null //output -> "object" //Javascript bug
Like you see, even JavaScript has bugs. Be careful using this value, because you can get unexpected behavior.
Undefined
Undefined is a value assigned to every variable that don’t has been assigned any other value, for example:
let undefinedVariable; //declared, assigned as undefined by default
console.log(undefinedVariable);
output // undefined
undefinedVariable = 4; // assigned, value changed to type Number
console.log(undefinedVariable)
output// 4
Important
Every time you are going to compare null with undefined you have to be careful about the automatic type conversion, because JavaScript will give you unexpected behavior, check out this:
console.log(null == undefined); //type conversion
output// true
console.log(null === undefined); //without type conversion
output// false
Take care and be patient with JavaScript, if you want to know more about it, check this out EloquentJavaScript.