Skip to content

JavaScript Operators

JavaScript uses operators to assign values, compare values, perform arithmetic operations, and more.

const x = 5; // Assign the value 5 to x
const y = 2; // Assign the value 2 to y
const z = x + y; // Assign the sum of x and y (7) to z

Arithmetic operators perform mathematical calculations on numbers (literals or variables).

OperatorDescriptionExample
+Addition10 + 5 results in 15
-Subtraction10 - 5 results in 5
*Multiplication10 * 5 results in 50
/Division10 / 5 results in 2
%Modulus (Remainder)10 % 3 results in 1
**Exponentiation (ES2016)10 ** 2 results in 100 (10 to the power of 2)
++Incrementlet a = 5; a++; now a is 6
—Decrementlet b = 5; b--; now b is 4

Increment (++) and decrement (--) operators can be postfix (x++) or prefix (++x). Postfix returns the value before incrementing/decrementing, while prefix returns the value after.

Assignment operators assign values to JavaScript variables.

OperatorExampleSame As
=x = yx = y
+=x += yx = x + y
-=x -= yx = x - y
*=x *= yx = x * y
/=x /= yx = x / y
%=x %= yx = x % y
**=x **= yx = x ** y (ES2016)

The simple assignment operator (=) assigns a value to a variable:

let score = 100;

The addition assignment operator (+=) adds a value to a variable and assigns the result back to the variable:

let currentScore = 50;
currentScore += 10; // currentScore is now 60

The + operator can also be used to concatenate (join) strings.

When used with strings, the + operator is called the concatenation operator.
const firstName = "John";
const lastName = "Doe";
const fullName = firstName + " " + lastName; // fullName is "John Doe"

The += assignment operator can also be used for string concatenation:

let greeting = "Hello ";
greeting += "World!"; // greeting is "Hello World!"

Modern JavaScript (ES6+) offers template literals (backticks `) for easier and more powerful string interpolation:

const name = "Alice";
const age = 30;
const message = `My name is ${name} and I am ${age} years old.`;
// message is "My name is Alice and I am 30 years old."

JavaScript’s type coercion rules apply when using the + operator with numbers and strings:

const sumNumbers = 5 + 5; // Result is 10 (number)
const concatStringNum = "5" + 5; // Result is "55" (string)
const concatHelloNum = "Hello" + 5; // Result is "Hello5" (string)

The general rule: If one of the operands of + is a string, JavaScript will convert the other operand to a string and perform concatenation.

Other arithmetic operators (-, *, /, %) will attempt to convert strings to numbers before performing the operation:

const subtractStrNum = "10" - 5; // Result is 5 (number, "10" converted to 10)
const multiplyStr = "5" * "2"; // Result is 10 (number, both converted)

JavaScript Comparison and Logical Operators

Section titled “JavaScript Comparison and Logical Operators”

Comparison operators are used in logical statements to determine equality or difference between variables or values. Logical operators are used to combine conditional statements.

OperatorDescriptionDetails & Example
==Equal toCompares values after type coercion. 5 == "5" is true.
===Strictly equal toCompares values AND types, no type coercion. 5 === "5" is false. Recommended for most equality checks.
!=Not equal toCompares values after type coercion. 5 != "5" is false.
!==Strictly not equal toCompares values AND types, no type coercion. 5 !== "5" is true. Recommended for most inequality checks.
>Greater than10 > 5 is true.
<Less than10 < 5 is false.
>=Greater than or equal to10 >= 10 is true.
<=Less than or equal to5 <= 10 is true.
OperatorDescriptionBehavior
&&Logical ANDexpr1 && expr2 returns expr1 if it can be converted to false; otherwise, returns expr2.
||Logical ORexpr1 || expr2 returns expr1 if it can be converted to true; otherwise, returns expr2.
!Logical NOT!expr returns false if expr can be converted to true; otherwise, returns true.

condition ? valueIfTrue : valueIfFalse Example: const access = age >= 18 ? 'granted' : 'denied';

Comparison and logical operators are fundamental for controlling program flow and are described in more detail in the JS Comparisons and Conditions chapters.