undefined
is a data type in JavaScript.
A variable declared using let
,
whose value hasn't
been assigned yet,
has undefined
as
it's default value.
In the above example, name
is assigned a value "Oliver"
and hence console.log(name)
displays "Oliver"
.
However, age
does not have
any value assigned to it.
That is why console.log(age)
displays undefined
,
which is the default value.
Note that a variable
declared using const
should always have a value
assigned to it.
It won't get
automatically assigned
with undefined
if no value is
given during declaration.
If we don't provide any value,
then we'll get a Syntax Error
like in the following example.