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.
Pause and think: What do you expect this code to do?
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.
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 , or .
Adding and subtracting numbers
Use the + operator to add numbers and the - operator to subtract numbers.
Multiplying and dividing numbers
Use the * operator to multiply numbers and the / operator to divide numbers.
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.
Test your understanding
What is the order of operations for the following variable?
var num3 = (10 + 10) * (10 - 10);
10 + 10 = 20 // Step 1
20 * 10 = 200 // Step 2
200 - 10 = 190 // Step 3
10 * 10 = 100 // Step 1
10 + 100 = 110 // Step 2
110 - 10 = 100 // Step 3
10 + 10 = 20 // Step 1
10 - 10 = 0 // Step 2
20 * 0 = 0 // Step 3
The modulo operator
The modulo operator % can be used to find the remainder of a division operation between two integer values.
Test your understanding
What number will the following operation yield?
-17 % 5
3.4
-3.4
An error
-2
2
Incrementing/decrementing number values
You can increase or decrease a number’s value by adding, subtracting, multiplying, or dividing it by another number:
You can simplify the above code by using +=, -=, *=, and /= shorthand operators to increase or decrease a number’s value:
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.
In this case, you can use the ++ and -- operators as a further shorthand: