Data Types
JavaScript supports three primitive data types: numbers, boolean values, and strings. In addition, it supports two compound data types: object and arrays. Functions are also a first-class data type in JavaScript, and JavaScript 1.2 adds support for regular expressions (described later) as a specialized type of object.
- Numbers
Numbers in JavaScript are represented in 64-bit floating-point format. JavaScript makes no distinction between integers and floating-point numbers. Numeric literals appear in JavaScript programs using the usual syntax: a sequence of digits, with an optional decimal point and an optional exponent. For example:
1 3.14 .0001 6.02e23
Integers may also appear in octal or hexadecimal notation. An octal literal begins with 0, and a hexadecimal literal begins with 0x:
0377 // The number 255 in octal 0xFF // The number 255 in hexadecimal
When a numeric operation overflows, it returns a special value that represents positive or negative infinity. When an operation underflows, it returns zero. When an operation such as taking the square root of a negative number yields an error or meaningless result, it returns the special value NaN, which represents a value that is not-a-number. Use the global function isNaN() to test for this value. The Number object defines useful numeric constants. The Math object defines various mathematical operations.
- Booleans
The boolean type has two possible values, represented by the JavaScript keywords
true
andfalse
. These values represent ...
Get Webmaster in a Nutshell, Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.