JavaScript Operators
Overview
In programming, operators are special symbols (characters) that are used to perform operations on operands (variables and values) and to process expressions. Most operators are used to perform mathematical operations such as addition, subtraction, multiplication, etc. However, there are other operators that are used to perform other operations such as assignment, comparison, logical, string, conditional, etc.
Prepare
It is essential if fundamental programming that you understand the types of operators.
Types of Operators
- Assignment operators are used to assign values to variables.
Example Assignment Operators
OperatorDescriptionExampleSame As=
Assigns a value to a variablex = y
x = y
+=
Adds a value to a variablex += y
x = x + y
-=
Subtracts a value from a variablex -= y
x = x - y
*=
Multiplies a variablex *= y
x = x * y
/=
Divides a variablex /= y
x = x / y
%=
Assigns a remainder to a variablex %= y
x = x % y
**=
Assigns an exponent to a variablex **= y
x = x ** y
&&=
Logical ANDx &&= y
x = x && y = y
||=
Logical ORx ||= y
x = x || (x = y)
- Arithmetic operators are used to preform arithmetic.
Example Arithmetic Operators
OperatorDescriptionExampleSame As+
Additionx + y
x = x + y
-
Subtractionx - y
x = x - y
*
Multiplicationx * y
x = x * y
/
Divisionx / y
x = x / y
%
Modulus (remainder)x % y
x = x % y
++
Incrementx++
x = x + 1
--
Decrementx--
x = x - 1
- Comparison - Tutorial
- Logical - important to create compound conditions as they determine the logic between expressions/values.
- Conditional (ternary) - alternative to if...else statement. Reference
- Spread (...) - this was used in the task last week. Reference
References
- Reference JavaScript Operators Reference - w3schools.com
- Reference More depth reference on Expressions and Operators
Check Your Understanding
- Operators and Comparisons - practice answering the questions and reviewing the examples.
Optional Resources
- Self Check: Using this article, Numbers, Operators, and Comparisons, can you complete the Test your skills: Math?
- Self Check: Using this article, Strings and Numbers, can you complete the Test your skills: Strings?