JavaScript Operators
Updated: May 10, 2020Numeric data in JavaScript is represented by the Number
data type.
Add Two Numbers
In JavaScript, like in mathematics, the +
symbol is used as an addition operator when in between two number.
Example:
Subtract One Number from Another
JavaScript uses the -
symbol for subtraction, in order to subtract one number from another:
Multiply Two Numbers
JavaScript uses the *
symbol for multiplication of two numbers:
Divide One Number by Another
JavaScript uses the /
symbol in order to divide one number by another:
Increment a Number
In JavaScript, we can use the ++
operator to increment or add one to a variable:
is the equivalent of
Decrement a Number
In JavaScript, we can use the --
operator to decrement or decrease a variable value by one:
is the equivalent of
Create Decimal Numbers
Variables can store also decimal number, which are referred to also as floating point numbers
or simply floats
.
Multiply Two Decimals
Just like with whole numbers, in JavaScript, we can perform calculations also with decimal numbers:
Divide One Decimal by Another
Now let's divide one decimal by another:
Finding a Remainder
In programming, it's common to check if a number is even
(divisible by 2) or odd
(not divisible by 2).
The remainder operator %
gives the remainder of the division of two numbers:
We can break this down by:
Compound Assignment With Augmented Addition
In programming, we use assignments to modify the content of a variable. Moreover, other than assigning a new value, we saw also how to perform basic mathematical operation.
In JavaScript, we can combine a mathematical operator and the assignment operator and obtain a new operator used for augmented addition +=
:
Compound Assignment With Augmented Subtraction
Just like the +=
operator we saw before, -=
subtracts a number from a variable:
Compound Assignment With Augmented Multiplication
Using the *=
operator, we can multiply a variable by a number:
Compound Assignment With Augmented Division
Using the /=
operator, we can divide a variable by another number: