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.
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.
Share Improve this question edited Mar 24, 2016 at 0:50 Peter O. 32.9k14 gold badges84 silver badges97 bronze badges asked Mar 23, 2016 at 8:22 ts248ts248 3612 gold badges5 silver badges11 bronze badges 3- 2 Literals – hindmost Commented Mar 23, 2016 at 8:24
- 1 @T.J.Crowder you're dead right. I was reading about it this morning on the way to work, and got it all mixed up :-(... ah well, it will stick now – Paul Fitzgerald Commented Mar 23, 2016 at 8:33
- Duplicate: Javascript null object type – Yogi Commented Mar 23, 2016 at 8:35
4 Answers
Reset to default 13Literals 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 munity-edited resource. It's usually pretty good, but it es 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 puter 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
null
is not an object , has no methods , and is absent of any value
See Null
In puter science, a null value represents a reference that points, generally intentionally, to a nonexistent or invalid object or address. The meaning of a null reference varies among language implementations.
In JavaScript, null is one of the primitive values.
Primitive
A primitive (primitive value, primitive data type) is data that is not an object and has no methods
4.3.12 null value
primitive value that represents the intentional absence of any object value
4.3.13 Null type
type whose sole value is the
null
value