JavaScript ES6 Operators
JavaScript operators are used in expressions to evaluate different operands. Expressions are statements that return values.
Here is an example of a simple JavaScript expression:
2 + 2
//returns 4
In this expression, we use the arithmetic operator + to add two operands (2 and 2). This expression returns 4.
Types of operators
Operators can be classified as arithmetic, relational, logical, bitwise, or assignment. Additionally, the ternary operator exists for evaluating conditional statements. Following is an explanation of each operator in more detail:
Arithmetic operators
These operators perform all arithmetic operations in JavaScript, including:
Addition +
2 + 2
//returns 4
Subtraction -
4 - 2
//returns 2
Multiplication *
5 * 5
//returns 25
Division /
4 / 2
//returns 2
Modulus %
6 % 5
//returns 1
Increment ++
2++
//returns 3
Decrement --
2--
//returns 1
Relational operators
These operators compare different values and return either true or false based on the expression. They include:
Greater than >
2 > 1
//returns true
Less than <
2 < 1
//returns false
Greater or equal to >=
2 >= 2
//returns true
Less than or equal to <=
1 <= 2
//returns true
Equal to ==
6 == 5
//returns false
Not equal to !=
2 != 3
//returns true
Logical Operators
Logical operators are used to combine conditions. They include:
And &&
true && false
//returns false because only one condition is true
Or ||
true || false
//returns true because at least one condition is true
Not !
!true
//returns false
Assignment Operators
These operators are used to change the value assigned to a given variable. These variables provide " short cuts" for assigning variables.
Simple Assignment =
x = 2
// assigns the value 2 to x
Add Assignment +=
var x = 2
x += 1
// x now equals 3
Subtract Assignment -=
var x = 2
x -= 1
// x now equals 1
Multiply Assignment *=
var x = 2
x *= 2
// x now equals 4
Divide Assignment /=
var x = 2
x /= 2
// x now equals 1
Bitwise Operators
Bitwise operators perform operations on the binary representation of their arguments. These operators are less commonly used and only relevant for hyper-performance programs and apps. They include:
And &
a & b
// returns a 1 in each bit position if bits of both a and b are 1
And &
a & b
// returns a 1 in each bit position if bits of both a and b are 1
Or |
a | b
// returns a 1 in each bit position if bit of either a or b is 1
XOr ^
a ^ b
// returns a 1 for each bit where one (but not the other) bit is 1
Not ~
~ a
// flips all the bits in a so 0 becomes 1 and 1 becomes 0
Left Shift <<
a << b
// shifts a in binary representation b bits to the left, shifting in zeros from the right
Right Shift >>
a >> b
// shifts a in binary representation b bits to the right, discarding bits shifted off
Zero-fill Right Shift >>>
a >>> b
// shifts a in binary representation b bits to the right, discarding bits shifted off, and shifting in zeros from the left.
If bitwise operators confuse you, that's ok. These are seldomly used in the real world and have very specific use cases. It should be noted that these operations still return a numeric (non-binary) value.
The Ternary Operator
This miscellaneous operator provides some syntactic sugar for writing an if..else statement in JavaScript.
true ? 2 : 3
// returns 2
This operator takes some getting used to, but can save valuable lines of code as an alternative to if..else statements. The example reads "if true, return 2, otherwise return 3".
typeof
This is another miscellaneous operator that returns the type of a given value. For example:
typeof 3
// returns 'number'
Checking for equality in JavaScript
Since = is reserved for assigning variables, we use == to compare equality. Furthermore, the == operator will implicitly convert types before checking for equality. To check equality without converting types, we use ===. For example:
0 == false //returns true
0 === false //returns false
The first statement returns true because the == operator implicitly converts false to it's numeric value 0 before comparing the two values. By using === we ignore this implicit conversion, resulting in false.
Conclusion
Operators are used in expressions to evaluate operands and are the basic building blocks of JavaScript logic. While arithmetic operators perform math based operation, assignment operators change the value of variables. Logical operators evaluate combined conditions. Bitwise operators are seldomly used but worth exploring.
Next, we'll look at Strings in JavaScript.