Introduction to Numbers

JavaScript has numbers like 8, 45607, and 4.32. The numbers can be added and subtracted:

8 + 4
4.3 - 1.1

Numbers can be multiplied and divided:

10 * 10
4 / 2

Multiple mathematical operations can be performed in a single statement. The following statement adds 1, 2, and 3:

1 + 2 + 3

Perform these exercises in the JavaScript console of your favorite web browser:

Question Click to View Answer

What does the following expression print to the console?

console.log(3 + 2);
5

3 and 2 are called operands and + is the operator.

What does the following expression print to the console?

console.log(3 + 4 * 2);
11

4 is multiplied by 2 first and the product is added to 3.

What does the following expression print to the console?

console.log(4.4 - 3.4);
1.0000000000000004

Numbers with decimal points (e.g. 4.4, 1.8) are called floating point numbers. Computers cannot do math with floating point numbers precisely, which is why the answer is slightly different from 1.

Integers are numbers without decimal points (e.g. 5 and 5,397). Computers can do math with integers precisely.

<-- Previous (Introduction to Strings) Next (Introduction to Booleans) -->