Why aren't they keywords? What are they?
true, false, null
Update Quick Answer
These are reserved words but they are not keywords.
Small technical distinction verified by spec - ES3 and ES5
Why aren't they keywords? What are they?
true, false, null
Update Quick Answer
These are reserved words but they are not keywords.
Small technical distinction verified by spec - ES3 and ES5
Share Improve this question edited Jul 19, 2017 at 15:41 janssen-dev 2,7712 gold badges30 silver badges55 bronze badges asked Sep 5, 2012 at 20:05 user656925user656925 2-
1
What makes you say they aren't reserved words?
true = 6
will never work, that kind of makes it a reserved word. – Niet the Dark Absol Commented Sep 5, 2012 at 20:08 - sorry...they are not keywords. – user656925 Commented Sep 11, 2012 at 22:07
4 Answers
Reset to default 11They are boolean literals. From the specification:
BooleanLiteral :: true false
- The value of the Boolean literal
true
is a value of the Boolean type, namelytrue
.- The value of the Boolean literal
false
is a value of the Boolean type, namelyfalse
.
It is similar to how 10
is a numeric literal or 'foo'
is a string literal.
Reserved words includes keywords and literals. The words true
and false
are reserved words, but they are not keywords. The following are keywords:
break do instanceof typeof
case else new var
catch finally return void
continue for switch while
debugger function this with
default if throw
delete in try
Notice that true
and false
don't appear in this list.
I think your confusion es from not realising that the two terms keyword and reserved word are not the same. Every keyword is a reserved word, but not every reserved word is a keyword.
Actually true
and false
are reserved words in Javascript, from:
http://ecma-international/ecma-262/5.1/#sec-7.6.1
A reserved word is an IdentifierName that cannot be used as an Identifier.
Syntax
ReservedWord ::
Keyword
FutureReservedWord
NullLiteral
BooleanLiteral
and in
http://ecma-international/ecma-262/5.1/#sec-7.8.2
you can read:
Boolean Literals
Syntax
BooleanLiteral ::
true
false
Mozilla Documentation
Additionally, the literals null, true, and false are reserved in ECMAScript for their normal uses.
They actually do appear to be "reserved" for usage, but I have no clue why they are not listed as a reserved word.
With non object javascript you just write
if( sami.value = true) //Noticed i didn't put ==
if it is object The Boolean object represents two values: "true" or "false".
The following code creates a Boolean object called myBoolean:
var myBoolean=new Boolean();
If the Boolean object has no initial value, or if the passed value is one of the following:
0
-0
null
""
false
undefined
NaN
the object is set to false. For any other value it is set to true (even with the string "false")!