Numbers
Get hands-on experience on how to use numbers as a JavaScript data type.
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.
var number1 = 10;var number2 = 20;console.log(number1 + number2);
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.
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 , or .
Adding and subtracting numbers
Use the + operator to add numbers and the - operator to subtract numbers.
console.log(50 + 50); //Addition operation on two numbersconsole.log(40 - 10); //Subtraction operation on two numbers
Multiplying and dividing numbers
Use the * operator to multiply numbers and the / operator to divide numbers.
// use the * operator to multiplyvar multiply = 10*10;// use the / operator to dividevar divide = 10 / 5;// operations on integers can result in floating point values, and vice versavar 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.
// * comes before + and -var num1 = 10 + 10 * 10 - 10;// operations in parenthesis have highest priorityvar num2 = (10 + 10) * 10 - 10;// operations occur left to right in order of operator precedencevar num3 = (10 + 10) * (10 - 10);console.log(num1, num2, num3);
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.
console.log(10 % 3); // 10 divides by 3 three times, with a remainder of 1console.log(10 % 4); // 10 divides by 4 twice, with a remainder of 2console.log(10 % 5); // 10 divides by 5 exactly twice, so no remainder
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:
var num = 10;// add 10 to the numbernum = num + 10;console.log("Add 10:", num);// subtract 5 from the numbernum = num - 5;console.log("Subtract 5:", num);// multiply the number by 7num = num * 7;console.log("Multiply by 7:", num);// divide the number by 5num = 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:
var num = 10;// add 10 to the numbernum += 10;console.log("Add 10:", num);// subtract 5 from the numbernum -= 5;console.log("Subtract 5:", num);// multiply the number by 7num *= 7;console.log("Multiply by 7:", num);// divide the number by 5num /= 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.
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:
var num = 10;num++;console.log(num);num--;console.log(num);