working with NUMBERS in JavaScript -Part 1 🤩
arithmetic operations that can be performed on numbers.
Hello! everyone 👋🏾.
Welcome, in this post we’ll be learning about data types in JavaScript specifically numbers.
Numbers in JavaScript are unsurprisingly values represented in a numeric format, For example 43, 5.4,-7. In JavaScript various kinds of operations can be performed on numbers. This post would cover performing simple arithmetic operations on numbers in JavaScript.
To perform operations in JavaScript, operators are used. Operators are special symbols used to perform operations on values and variables.
Operations on numbers
Addition
To perform addition on numbers you use the plus symbol (+
). +
is called the addition operator. For instance:
// to add 23 and 46
let add = 23 + 46
/* variables storing numbers
can also be operated upon */
let initial = 30;
let acquired = 20;
let total = initial + acquired;
Incrementing a number:
incrementing a number simply means increasing the number by 1. To do so, use the double plus sign (++
). For instance:
// to increment 6
let num = 6;
num ++;
Subtraction
To perform subtration on numbers, use the minus symbol (-
). -
is called the subtraction operator. For instance:
// to subtract 23 from 46
let sub = 46 - 23;
let initial = 30;
let lost = 20;
let total = initial - lost;
Decrementing a number:
decrementing a number simply means decreasing the number by 1. To do so, use the double minus sign (- -
). For instance:
// to decrement 6
let num = 6;
num - -;
Multiplication
To perform multiplication on numbers, use the asterisk symbol (*
). *
is called the multiplication operator. For instance:
// to multiply 3 and 6
let multiply = 3 * 6;
let initial = 30;
let factor = 5;
let result = initial * factor;
console.log(result)
Division
To perform division on numbers, use the forward slash symbol (/
). /
is called the division operator. For instance:
// to divide 6 by 3
let div = 6 / 3;
let dividend = 30;
let divisor =4;
let quotient = dividend / divisor;
Modulous
To perform modulo division on numbers, use the percentage symbol
(%
). %
is called the remainder operator. For instance:
/* to find the remainder when
7 is divided by 3 */
let mod = 7 % 3;
let dividend = 30;
let divisor = 4;
let remainder = dividend % divisor;
console.log(remainder)
Exponent
To perform exponential operation ie 2 3 on numbers you use the exponential operator (**
). For instance:
// to find 2 exponent 3
let exponent = 2**3
let base = 3;
let power = 2;
let exponent = base ** power;
// NOTE
2 ** 3 ** 2 === 2 ** (3 ** 2)
Precedence of operators
Precedence of operators refers to the order in which operations are carried out when two or more operators appear at the same time. Here are rules for the precedence of arithmetic operators.
Exponential operations are performed first before multiplication, division, remainder, addition and subtraction
multiplication, division and remainder are evaluated before addition and subtraction. For example
6 + 3 * 2
results in 12.If multiplication, division and remainder operators appear in the same statement, then they are evaluated depending on what comes first starting from left to right ie
6 * 3 / 2
result is 9. And6 / 3 * 2
result is 4.If addition and subtraction operators appear in the same statement, then they are evaluated depending on what comes first starting from left to right ie
6+5-3
answer is 8.
Grouping:
The order of operation can be changed by introducing parenthesis (grouping operator) in mathematical statements. Whatever is between the parenthesis is evaluated first before any other operation takes place. for example 6 * 8 + 2
would result in 50 (multiplication is performed first) but when grouping is introduced ie 6 * (8 + 2)
it would result in 60. Addition would be performed before multiplication because of the presence of the parentheses (grouping operator).
That’s a wrap! Thank you for reading this post, hopefully it was useful. Kindly share your thoughts in the comment section. Click the follow button for useful posts on learning and mastering JavaScript as well as other technologies in web development!.
- follow me on Twitter at @a__sally