Any value that you use is of a certain type. In JavaScript, there are just a few primitive data types:
1
, 100
, 3.14
."a"
, "one"
, and "one 2 three"
.true
or false
.undefined
. The same happens when you declare a variable without assigning a value to it yet. JavaScript initializes the variable behind the scenes with the value undefined
. The undefined data type can only have one value – the special value undefined
.null
value. It means no value, an empty value, or nothing. The difference with undefined
is that if a variable has a value null
, it's still defined, it just so happens that its value is nothing. You'll see some examples shortly.Any value that doesn't belong to one of the five primitive types listed here is an object. Even null
is considered an object, which is a little awkward—having an object (something) that is actually nothing. We'll learn more on objects in Chapter 4, Objects, but for the time being, just remember that in JavaScript the data types are either:
If you want to know the type of a variable or a value, you use the special typeof
operator. This operator returns a string that represents the data type. The return values of using typeof
are one of the following:
"number"
"string"
"boolean"
"undefined"
"object"
"function"
In the next few sections, you'll see typeof
in action using examples of each of the five primitive data types.
The simplest number is an integer. If you assign 1
to a variable and then use the
typeof
operator, it returns the string "number"
:
> var n = 1; > typeof n; "number" > n = 1234; > typeof n; "number"
In the preceding example, you can see that the second time you set a variable's value, you don't need the var
statement.
Numbers can also be floating point (decimals):
> var n2 = 1.23;
> typeof n;
"number"
You can call typeof
directly on the value without assigning it to a variable first:
> typeof 123;
"number"
When a number starts with a 0, it's considered an octal number. For example, the octal 0377
is the decimal 255
:
> var n3 = 0377; > typeof n3; "number" > n3; 255
The last line in the preceding example prints the decimal representation of the octal value.
While you may not be intimately familiar with octal numbers, you've probably used hexadecimal values to define colors in CSS stylesheets.
In CSS, you have several options to define a color, two of them being:
rgb(0, 0, 0)
is black and rgb(255, 0, 0)
is red (maximum amount of red and no green or blue).#000000
is black and #ff0000
is red. This is because ff
is the hexadecimal value for 255.In JavaScript, you put 0x
before a hexadecimal value (also called hex for short):
> var n4 = 0x00; > typeof n4; "number" > n4; 0 > var n5 = 0xff; > typeof n5; "number"> n5; 255
1e1
(also written as 1e+1
or 1E1
or 1E+1
) represents the number one with one zero after it, or in other words, 10. Similarly, 2e+3
means the number 2 with 3 zeros after it, or 2000:
> 1e1; 10 > 1e+1; 10 > 2e+3; 2000 > typeof 2e+3; "number"
2e+3
means moving the decimal point three digits to the right of the number 2. There's also 2e-3
, meaning you move the decimal point three digits to the left of the number 2:
> 2e-3; 0.002 > 123.456E-3; 0.123456 > typeof 2e-3; "number"
There is a special value in JavaScript called Infinity
. It represents a number too big for JavaScript to handle. Infinity
is indeed a number, as typing typeof Infinity
in the console will confirm. You can also quickly check that a number with 308 zeros is ok, but 309 zeros is too much. To be precise, the biggest number JavaScript can handle is 1.7976931348623157e+308
, while the smallest is 5e-324
.
> Infinity; Infinity > typeof Infinity; "number" > 1e309; Infinity > 1e308; 1e+308
Dividing by zero gives you infinity:
> var a = 6 / 0;
> a;
Infinity
Infinity is the biggest number (or rather a little bigger than the biggest), but how about the smallest? It's infinity with a minus sign in front of it; minus infinity:
> var i = -Infinity; > i; -Infinity > typeof i; "number"
Does this mean you can have something that's exactly twice as big as Infinity
, from 0 up to infinity and then from 0 down to minus infinity? Well, not really. When you sum infinity and minus infinity, you don't get 0, but something that is called NaN
(Not a Number):
> Infinity – Infinity; NaN > -Infinity + Infinity; NaN
Any other arithmetic operation with Infinity
as one of the operands gives you Infinity
:
> Infinity – 20; Infinity > -Infinity * 3; -Infinity > Infinity / 2; Infinity > Infinity – 99999999999999999; Infinity
What was this NaN
in the previous example? It turns out that despite its name, "Not a Number", NaN
is a special value that is also a number:
> typeof NaN; "number" > var a = NaN; > a; NaN
You get NaN
when you try to perform an operation that assumes numbers, but the operation fails. For example, if you try to multiply 10
by the character "f"
, the result is NaN
, because "f"
is obviously not a valid operand for a multiplication:
> var a = 10 * "f";
> a;
NaN
NaN
is contagious, so if you have even one NaN
in your arithmetic operation, the whole result goes down the drain:
> 1 + 2 + NaN;
NaN
3.145.156.46