javascript interactive

JavaScript Interactive — a console crash course


1.1 Primitive Types

Warning: Your browser may be outdated or unsupported.

The JavaScript console is known to display some of the examples incorrectly in Internet Explorer 8 or older.

The JavaScript language defines five primitive data types:

  • undefined – Used when no value is set.
  • null – Used for blank or empty values. One value, the constant null.
  • boolean – Used for logical values. Two values, the constants true & false.
  • number – Used for numeric values. Floating-point 64-bit IEEE 754 values.
  • string – Used for text values. UTF-16 encoded (16 bit Unicode) values.
  • object – Used for everything else. More details on the next page.
>>>
Object.doesNotExist
undefined
>>>
null
null
>>>
true
true
>>>
4711.0
4711
>>>
1.5E-5
0.000015
>>>
42 / 9
4.666666666666667
>>>
0.1 + 0.2
0.30000000000000004
Ouch! As numbers are always floating-point, rounding-errors like this will lurk behind every corner.
>>>
0 / 0
NaN
The special NaN (Not A Number) numeric value is sometimes also a cause for problems.
>>>
"a 'string'"
"a 'string'"
>>>
'another "string"'
"another \"string\""
There are two syntaxes for string literals (using " or '), but both have exactly the same meaning.
>>>
"\061, \x32, \u0033"
"1, 2, 3"
Octal and hexadecimal (2 & 4 bytes) character escapes are available.
>>>

Index 1 2 3 4 5 6 7