JavaScript operators are special symbols and keywords that perform operations on values and variables, serving as fundamental building blocks for data manipulation and expression creation.
These operators are organized into several key categories: arithmetic operators for mathematical calculations, assignment operators for value management, comparison operators for evaluating relationships, logical operators for boolean logic, and specialized operators like typeof for data type determination.
Arithmetic Operators (+, -, *, /)
Arithmetic operators form the core of mathematical operations in JavaScript, enabling developers to perform calculations with numerical values. The basic arithmetic operators include addition (+
) for combining values, subtraction (-
) for finding differences, multiplication (*
) for finding products, and division (/
) for calculating quotients. These operators work with both literal numbers and variables, following standard mathematical conventions where operations like 5 + 3
return 8
and 10 / 2
returns 5
.
Beyond the fundamental four operations, JavaScript provides additional arithmetic operators that extend mathematical functionality:
Remainder/Modulus (
%
) - Returns the remainder after division, such as10 % 3
returning1
Exponentiation (
**
) - Raises a base number to a specified power, where5 ** 2
equals25
Increment (
++
) and Decrement (--
) - Add or subtract 1 from a variable, with prefix and postfix variations affecting when the operation occursUnary plus (
+
) and Unary negation (-
) - Convert operands to numbers or negate values respectively
All arithmetic operations in JavaScript are performed using double-precision floating-point numbers, meaning calculations like 1/2 produce 0.5 rather than integer results. These operators follow standard mathematical precedence rules, where multiplication and division are evaluated before addition and subtraction unless parentheses override the order
Assignment Operators (=, +=, *=)
Assignment operators provide an efficient way to assign values to variables and perform operations simultaneously, streamlining code by combining assignment with arithmetic calculations. The simple assignment operator (=
) forms the foundation by assigning the value on its right to the variable on its left, such as let x = 10
. This operator evaluates the right-hand expression first, then stores the result in the left-hand variable.
Compound assignment operators combine arithmetic operations with assignment to create more concise expressions. The addition assignment operator (+=
) adds a value to the existing variable content, where x += 5
is equivalent to x = x + 5
. Similarly, the multiplication assignment operator (*=
) multiplies the variable by the specified value, so x *= 3
becomes x = x * 3
. These compound operators work by evaluating the left operand only once, making them slightly more efficient than their expanded equivalents. Other compound assignment operators include subtraction assignment (-=
), division assignment (/=
), remainder assignment (%=
), and exponentiation assignment (**=
), each following the same pattern of performing the operation and storing the result back in the original variable.
Comparison Operators (==, !=, >)
Comparison operators evaluate relationships between values and return boolean results (true
or false
), making them essential for conditional logic and decision-making in JavaScript programs. The equality operator (==
) performs loose comparison by checking if two values are equal after converting them to the same type when necessary. For example, 5 == '5'
returns true
because JavaScript converts the string '5'
to the number 5
before comparison, and 0 == false
also returns true
due to type coercion. The inequality operator (!=
) works as the opposite of equality, returning true
when values are not equal after type conversion, so 5 != '5'
returns false
while 5 != 10
returns true
.
Relational operators compare the magnitude of values, with the greater than operator (>
) checking if the left operand is larger than the right operand. These operators include:
Greater than (
>
) - Returnstrue
if left value is larger:8 > 5
equalstrue
Less than (
=
) - Returnstrue
if left value is larger or equal:5 >= 5
equalstrue
Less than or equal (
<=
) - Returnstrue
if left value is smaller or equal:4 <= 6
equalstrue
These comparison operators automatically perform type coercion when comparing different data types, which can lead to unexpected results in some cases. Understanding this behavior is crucial for writing reliable conditional statements and loops that depend on accurate value comparisons.