Search⌘ K
AI Features

Numbers

Explore JavaScript numeric data types and arithmetic operators. Understand how to add, subtract, multiply, divide, and use increment and decrement operators to manipulate numbers in your code.

Context: JavaScript data types

When it comes to programming, pretty much everything we do revolves around storing and manipulating data. This data always has a type associated with it that tells the computer exactly how to handle the data that it’s given. In JavaScript, the data’s type is automatically determined when the code is executed.

What exactly do we mean by this? Let’s take a look at an example.

Node.js
var number1 = 10;
var number2 = 20;
console.log(number1 + number2);
1.

Pause and think: What do you expect this code to do?

Show Answer
Did you find this helpful?

For your reference, console.log() outputs to the console whatever data is passed to it in between the parenthesis. You can run the code below to see the output.

Node.js
var number1 = 10;
var number2 = 20;
console.log(number1 + number2);

Numbers

Numbers are a numeric data type. Numbers can be integers, floating-point (numbers with a decimal), or exponential values like those you would find with scientific notation.

All numbers in JavaScript are stored using 64 bits of memory. While delving into how computers represent numbers is out of the scope of this course, it’s important to know that there are finite limits to the size of number you can store.

In practice, this shouldn’t be an issue, unless you’re working with really, really large numbers. Each bit represents a binary value (e.g., 0 or 1). With 64 bits, the maximal integer you can represent is 26312^{63} - 1, or 92233720368547758079223372036854775807.

Adding and subtracting numbers

Use the + operator to add numbers and the - operator to subtract numbers.

Node.js
console.log(50 + 50); //Addition operation on two numbers
console.log(40 - 10); //Subtraction operation on two numbers

Multiplying and dividing numbers

Use the * operator to multiply numbers and the / operator to divide numbers.

Node.js
// use the * operator to multiply
var multiply = 10*10;
// use the / operator to divide
var divide = 10 / 5;
// operations on integers can result in floating point values, and vice versa
var float = 10 / 3;
console.log("Ten times ten equals", multiply);
console.log("Ten divided by five equals", divide);
console.log("Ten divided by three equals", float);

Order of operations

Operations take place from left to right in order of precedence, meaning certain operators have a higher priority than others. In general:

  • Any operations placed in parenthesis, (), will take place first
  • Multiplication (*) and division (/) operations will take place before addition (+) and subtraction (-) operations.
Node.js
// * comes before + and -
var num1 = 10 + 10 * 10 - 10;
// operations in parenthesis have highest priority
var num2 = (10 + 10) * 10 - 10;
// operations occur left to right in order of operator precedence
var num3 = (10 + 10) * (10 - 10);
console.log(num1, num2, num3);

Test your understanding

1.

What is the order of operations for the following variable?

var num3 = (10 + 10) * (10 - 10);
A.
10 + 10 = 20 // Step 1
20 * 10 = 200 // Step 2
200 - 10 = 190 // Step 3
B.
10 * 10 = 100 // Step 1
10 + 100 = 110 // Step 2
110 - 10 = 100 // Step 3
C.
10 + 10 = 20 // Step 1 
10 - 10 = 0 // Step 2
20 * 0 = 0 // Step 3

1 / 2

The modulo operator

The modulo operator % can be used to find the remainder of a division operation between two integer values.

Node.js
console.log(10 % 3); // 10 divides by 3 three times, with a remainder of 1
console.log(10 % 4); // 10 divides by 4 twice, with a remainder of 2
console.log(10 % 5); // 10 divides by 5 exactly twice, so no remainder

Test your understanding

1.

What number will the following operation yield?

-17 % 5
A.
3.4
B.
-3.4
C.
An error
D.
-2 
E.
2

1 / 1

Incrementing/decrementing number values

You can increase or decrease a number’s value by adding, subtracting, multiplying, or dividing it by another number:

Node.js
var num = 10;
// add 10 to the number
num = num + 10;
console.log("Add 10:", num);
// subtract 5 from the number
num = num - 5;
console.log("Subtract 5:", num);
// multiply the number by 7
num = num * 7;
console.log("Multiply by 7:", num);
// divide the number by 5
num = num / 5;
console.log("Divide by 5:", num);

You can simplify the above code by using +=, -=, *=, and /= shorthand operators to increase or decrease a number’s value:

Node.js
var num = 10;
// add 10 to the number
num += 10;
console.log("Add 10:", num);
// subtract 5 from the number
num -= 5;
console.log("Subtract 5:", num);
// multiply the number by 7
num *= 7;
console.log("Multiply by 7:", num);
// divide the number by 5
num /= 5;
console.log("Divide by 5:", num);

These operations are often times referred to as incrementing or decrementing a value:

  • In the case of +=, the value is being incremented by a constant value.
  • In the case of -=, the value is being decremented by a constant value.
  • In the case of *=, the value is being incremented by a multiple.
  • In the case of /=, the value is being decremented by a multiple.

++ and -- operators

There are many instances where you will want to increment or decrement a value by one.

Node.js
var num = 10;
num += 1;
console.log(num);
num -= 1;
console.log(num);

In this case, you can use the ++ and -- operators as a further shorthand:

Node.js
var num = 10;
num++;
console.log(num);
num--;
console.log(num);