JavaScript Operators
Operators in JavaScript are special symbols used to perform operations on values. They can be used for arithmetic calculations, comparisons, logical operations, etc. Here are some of the most common types of operators in JavaScript:
- Arithmetic Operators
- Comparison Operators
- Logical Operators
- Assignment Operators (covered later)
- String Operators
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations.
Addition (+
): Adds two numbers.
5 + 3 // 8
Subtraction (-
): Subtracts the second number from the first.
9 - 4 // 5
Multiplication (*
): Multiplies two numbers.
6 * 7 // 42
Division (/
): Divides the first number by the second.
8 / 2 // 4
Modulus (%
): Returns the remainder of a division.
10 % 3 // 1
Comparison Operators
Comparison operators are used to compare two values and return a boolean (true
or false
).
Equal (==
): Checks if two values are equal.
5 == 5 // true 5 == '5' // true
Strict Equal (===
): Checks if two values are equal and of the same type.
5 === 5 // true 5 === “5" // false
Notice, this time, the string ”5"
is not equal to 5
. Strict operators don’t try to match the data and expect them to be precisely the same.
Not Equal (!=
): Checks if two values are not equal.
5 != 3 // true 5 != '5' // false
Strict Not Equal (!==
): Checks if two values are not equal or not of the same type.
5 !== 5 // false 5 !== '5' // true
Greater Than (>
): Checks if the first value is greater than the second.
7 > 5 // true
Less Than (<
): Checks if the first value is less than the second.
3 < 4 // true
Greater Than or Equal (>=
): Checks if the first value is greater than or equal to the second.
7 >= 7 // true 7 >= 5 // true
Less Than or Equal (<=
): Checks if the first value is less than or equal to the second.
5 <= 5 // true 3 <= 4 // true
Logical Operators
Logical operators are used to combine multiple boolean expressions or values and return a boolean result.
AND (&&
): Returns true if both expressions are true.
true && true // true true && false // false
OR (||
): Returns true if at least one expression is true.
true || false // true false || false // false
NOT (!
): Inverts the value of a boolean expression.
!true // false !false // true
String Operators
Strings can be concatenated (joined together) using the addition (+
) operator.
Concatenation (+
): Joins two or more strings together.
'Hello, ' + 'World!' // 'Hello, World!'
Exercise
Here are some examples of using these operators. Run each one in the browser, but try to guess the output before you run it:
Arithmetic Operations:
2 + 3 10 - 7 4 * 5 20 / 4 15 % 4
Comparison Operations:
5 == '5' 5 === '5' 10 != 8 6 !== '6' 9 > 3 7 < 10 8 >= 8 4 <= 5
Logical Operations:
true && false true || false !true
String Concatenation:
'JavaScript ' + 'is fun!'
I’ll leave the answers below if you want to confirm your suspicions.
Operators are essential for performing various operations on values and controlling the flow of your programs. They are fundamental tools for writing logical and functional code in JavaScript and nearly every programming language.
Next, we will explore expressions in JavaScript, which combine values and operators to produce a new value.
Answers to above exercise
Arithmetic Operations:
2 + 3 // 5 10 - 7 // 3 4 * 5 // 20 20 / 4 // 5 15 % 4 // 3
Comparison Operations:
5 == '5' // true 5 === '5' // false 10 != 8 // true 6 !== '6' // true 9 > 3 // true 7 < 10 // true 8 >= 8 // true 4 <= 5 // true
Logical Operations:
true && false // false true || false // true !true // false
String Concatenation:
'JavaScript ' + 'is fun!' // 'JavaScript is fun!'