In this post, we are going to discuss what is undefined
, not-defined
, null
and their behaviours.
undefined
javascript assigns undefined
to any variable that has been declared but not given a value, in other words, undefined acts as a placeholder when you don't explicitly initialize it during declaration.
here are some examples.
let x;
console.log(x); // undefined
console.log(x === undefined); // true
not defined
when you get a ReferenceError: variable is not defined
it means you didn't declare that variable at that point in time.
its an error in javascript.
let a = 10
console.log(a + x) // ReferenceError: a is not defined
as you can see in the above code when we tried to access x
which is not declared anywhere, we get an error of not defined
simply means javascript can't find any variable named x
in the code.
Null
the value null
represents the intentional absence of a value for a variable. it is treated as falsy for boolean values.
you can assign null
to a variable to denote that currently, that variable doesn't have any value but it will have later on
let myVar = null
console.log(myVar) // null
console.log(!null) // true
in this code you can see !null
prints true since null
is treated as a falsy value by default.
in short if null is assigned that means the value is absent.
I'm glad you took the time to read this post ๐