undefined
is a global variable which has the value undefined
— a datatype by itself which can have only one value. But what is the value null
?
MDN says:
The value null is a literal.
What does that mean?
I know literal as "something you write fixed into your code". Like x = 3 + 4.
Here 3 and 4 would be integer-literals. But I don't get that together with what the MDN documentation says.
The value null is written with a literal: null . null is not an identifier for a property of the global object, like undefined can be. Instead, null expresses a lack of identification, indicating that a variable points to no object.
The null keyword is a literal that represents a null reference, one that does not refer to any object. null is the default value of reference-type variables. Ordinary value types cannot be null, except for nullable value types.
Literals represent values in JavaScript. These are fixed values—not variables—that you literally provide in your script.
In JavaScript, null is a special value that represents an empty or unknown value. For example, let number = null; The code above suggests that the number variable is empty at the moment and may have a value later.
Literals are just a way to write values in our code. null
, the thing you actually type, is a literal that defines the value null
, just like 1
is a literal that defines the value 1. The null
value is just that: A value (the only value in the Null type), like 1 is a value (one of many values in the Number type).
Remember that MDN is a community-edited resource. It's usually pretty good, but it comes down to who wrote and/or subsequently edited the page. Sometimes the terminology is a bit off. Let's look at the opening sentence of that page as it was when you posted your question:
The value
null
is a JavaScript literal representing null or an "empty" value, i.e. no object value is present.
Yeah, that looks like an editing error. It should read something like:
The value
null
represents the intentional absence of any object value.
I'll probably edit it later. I've edited it and the subsequent line that's trying to highlight that null
is a literal, not an identifier for a property on the global object like undefined
can be.
I am no expert but I think you already have the answer: you said 3 and 4 are "integer-literal". Quoting wikipedia:
In computer science, a literal is a notation for representing a fixed value in source code.
So 3
and 4
are both literals, as well as 34
or "hello"
. null
is a literal in the same way, but instead of representing a number or a string, is has a meaning of nothingness.
EDIT: As I said, I am no expert and it seems I am not quite right about literals. T.J. Crowder explains it much better than me, see his answer
Null
is one of the primitives in JS.
This primitive type is used as a literal in some cases which need to represent an absence of a value in a variable or reference.
However said, the literal value of null
has a special meaning.
Point worth noting is that:
typeof null // object (bug in ECMAScript, should be null)
typeof undefined // undefined
null === undefined // false
null == undefined // true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With