Operators
There are different types of operators in JavaScript. Some operators are unary operators, they work on a single operand. Others are binary operators, they require two operands. There is also a ternary operator in JavaScript. Ternary operators require three operands. Next let's review these operators.
The typeof
operator
There is an operator in JavaScript called the typeof
operator. It is used to check the data type of a variable.
To use it, you specify the name of the variable in parentheses after the keyword as in typeof(variableName)
. It is a unary operator.
Arithmetic Operators
These operators are used to carry out mathematical operations.
- Addition: The
+
operator is used to add two numbers together:16 + 7
. - Subtraction: The
-
operator subtracts the right number from the left:9 - 8
. - Multiplication: The
*
operator multiplies two numbers together:9 * 5
. - Division: The
/
operator divides the left number by the right number:54 / 5
. - Remainder (modulo): The
%
operator returns the remainder after a division:18 % 3
. - Exponent: The
**
operator, also known as the power operator, raises a base number to the exponent:3 ** 2 = 9
Activity
Operator Precedence
With respect to the arithmetic operators, the exponent operator has the highest level of precedence. Division /
and multiplication *
come next, and they have a higher precedence to addition and subtraction (which are both on the same level of precedence). When operators on the same level of precedence are used, the order of execution is from left to right.
Further Reading
Read more on Operator Precedence here
Increment and Decrement Operators
JavaScript also has the increment and decrement operators to easily add or subtract one from a numeric value.
Try it in the console:
let x = 9;
let y = 8;
x++;
y--
console.log(x)
console.log(y)
Assignment Operators
Assignment operators are used to assign a value to a variable. We have seen how to use =
severally, it assigns the value on the right to the variable on the left. Apart from the basic =
, other complex assignment operators exist.
For example, the addition assignment operator, +=
, it adds the values on the right to the variable on the left, and assigns the new value to the variable. In like manner, we have the subtraction assignment operator, -=
, the multiplication assignment operator, *=
, and the division assignment operator, /=
.
Try it!
let x = 8;
console.log(x += 8)
console.log(x -= 6)
console.log(x *= 4)
console.log(x /= 2)
Comparison Operators
Similar to the Python programming language, there are several comparison operators in JavaScript that can be used to run true/false tests. They always return one of the two Boolean values: true
or false
. The code snippet below shows examples
Logical Operators
Logical operators are used to compare Boolean values. JavaScript supports three comparison operators, namely: and
, or
, and not
. They are binary operators, with the exception of the not
operator which is a unary operator.
The logical and
operator
The &&
operator represents the logical and
. It returns true
only if both values given to it are true, otherwise it returns false
.
The logical or
operator
The ||
represents logical or
. It returns true
if any of the operands given to it are true. It only returns false
when both operands given to it are false.
The logical not
operator
The !
, represents the logical not
. It simply flips or negates the value given to it.
The conditional
operator
The JavaScript language has one ternary operator. A ternary operator is one that operates on three operands (values). It is called the conditional
operator. Take a look at the code snippet below to see how it operates.
console.log(true ? "it's true" : "it's false" )
//it's true
console.log(false ? "it's true" : "it's false" )
//it's false
console.log(true ? 1 : 2 )
//Your turn. What do you think the output will
The value on the left of the question mark βpicksβ which of the other two values will come out. When it is true, it chooses the middle value, and when it is false, it chooses the value on the right.
Further Reading
There are several other JavaScript operators that exist. Here's a list of them. The link also contains details on the precedence of all the operators.
Activity
ππΏ Try it: Practice the arithmetic operators by drawing different boxes here.