Primitive data types

Any value that you use is of a certain type. In JavaScript, there are just a few primitive data types:

  1. Number: This includes floating point numbers as well as integers. For example, these values are all numbers: 1, 100, 3.14.
  2. String: These consist of any number of characters, for example "a", "one", and "one 2 three".
  3. Boolean: This can be either true or false.
  4. Undefined: When you try to access a variable that doesn't exist, you get the special value 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.
  5. Null: This is another special data type that can have only one value, namely the 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:

  • Primitive (the five types listed previously)
  • Non-primitive (objects)

Finding out the value type – the typeof operator

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.

Numbers

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"

Octal and hexadecimal numbers

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:

  • Using decimal values to specify the amount of R (red), G (green), and B (blue) ranging from 0 to 255. For example, rgb(0, 0, 0) is black and rgb(255, 0, 0) is red (maximum amount of red and no green or blue).
  • Using hexadecimals and specifying two characters for each R, G, and B value. For example, #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

Exponent literals

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:

Exponent literals
> 2e-3;
0.002
> 123.456E-3;
0.123456
> typeof 2e-3;
"number"

Infinity

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

NaN

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
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.156.46