Hello. What is the difference between undefined and null?
Undefined means that a variable has been declared but not defined:
let a;
console.log(a); //prints undefined
Null is an assigned value. Itâs a value of nothing.
let a = null;
Null is also a type of object, while undefined is a type of undefined.
(Note that you can also assign undefined to a variable.)
let a = undefined;
Hi, I like to think of undefined in JavaScript as missing (value). Hope that helps.
Thank you for your replies.
To check null in JavaScript, use triple equals operator(===) or Object.is() method. To find the difference between null and undefined, use the triple equality operator or Object.is() method.
To loosely check if the variable is null, use a double equality operator(==). The double equality operator can not tell the difference between null and undefined, so it counts as same. So if you want to strict check, then donât use a double equality operator. It will lead to misconclusion.
Not the formal definition, but I like to see it this way:
Undefined
means that something exists, but isnât meaningful
Null
means it doesnât exist, or defined to be nothing.
I might be wrong on this thoughâŚ
A lot of mythology in this thread
To put it simply, Null is the âENDâ. Computers constantly deal with sequences of stuff (instructions in functions, values in objects or arrays etc.) and they need a way to be able to move along imaginary line between instructions in both directions. This raises a question, how computers will know where are the boundaries of each particular sequence , right? This question was solved by introduction of Null (or sometimes Nil) Pointer to a special address in memory that programs wonât be able to rewrite and therefore regardless of a program this rule will stand. In order for programs to be written every programming language must therefore have Null in their syntax and JavaScript isnât an exception. Null in JavaScript points to the same point in memory as Null in Python, C++ or BashScript. Now back to present days, JavaScript doesnât deal on those low levels and never allows programmer to set memory boundaries in any way, so usage of Null is âimaginaryâ, meaning that you can create tree of stuff and you will put Null to define boundaries of that tree (just like DOM) to mimic how things are done in memory
JavaScript doesnât need to âconstructâ Null it simply uses an existing pointer. Unlike Null, Undefined is a custom construct of JavaScript and therefore has to start somewhere. As all other constructs it starts at global object (window in browser) and therefore you can do silly stuff like this:
function CallMeAndGoodLuck() {
Array = null;
Number = null;
undefined = null;
Function = null;
Object = null;
// etc...
}
Just like all C-family languages (and probably others too), languages need Undefined to express uninitialized variables:
let a; // uninitialized variable
typeof a; // undefined
To put it simply, Undefined is âEMPTYâ.
Regarding typeof null === 'object'
, hopefully what Iâve written above clearly shows that this was a bug initially and because of philosophy of JavaScript around backward compatibility it never had itâs chance to be fixed. By absolutely no means Null is an object and if you need a proof, try this:
// Every object-like structure in JavaScript is an instance of Object
new Date() instanceof Object; // true
[] instanceof Object; // true
(function fn() {}) instanceof Object; // true
(() => {}) instanceof Object; // true
new Boolean('false') instanceof Object; // true
null instanceof Object; // false
typeof null; // "object" đ
Iâve heard a story (I donât know if itâs true) that in early days objects in JavaScript had memory allocated at addresses that started with â0x0âŚâ and the way typeof was calculated is by checking if address starts with zero. Guess what has the address 0x0000
in computer memory?
Thanks, really amazing answer :). I actually understand what is happening now!