Skip to main content

Javascript Operators

In JavaScript, operators are symbols that are used to perform operations on values, such as arithmetic operations, logical operations, and comparison operations.

Here are some of the most commonly used operators in JavaScript:

Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations on numeric values.

The basic arithmetic operators are:

+   // Addition
- // Subtraction
* // Multiplication
/ // Division
% // Modulus (returns the remainder of a division operation)

As an example:

let x = 5;
let y = 2;
let z = x + y; // z = 7
let w = x % y; // w = 1

Comparison Operators

Comparison operators are used to compare two values and return a Boolean value (true or false).

The most common comparison operators are:

==  // Equal to (checks for equality, but does not check for type)
=== // Strictly equal to (checks for equality and type)
!= // Not equal to (checks for inequality, but does not check for type)
!== // Strictly not equal to (checks for inequality and type)
< // Less than
> // Greater than
<= // Less than or equal to
>= // Greater than or equal to

As an example:

let x = 5;
let y = 2;
console.log(x > y); // true
console.log(x == "5"); // true
console.log(x === "5"); // false

Logical Operators

Logical operators are used to combine Boolean values and return a Boolean result.

The most common logical operators are:

&&  // Logical AND (returns true if both operands are true)
|| // Logical OR (returns true if at least one operand is true)
! // Logical NOT (inverts the value of a Boolean expression)

As an example:

let x = 5;
let y = 2;
let z = 3;
console.log(x > y && z > y); // true
console.log(x < y || z < y); // false
console.log(!(x > y)); // false

Assignment Operators

Assignment operators are used to assign values to variables.

The basic assignment operator is the equals sign (=), but there are also shorthand assignment operators that combine an arithmetic operator with the equals sign.

The most common arithmetic operators are:

=   // Assign
+= // Add and assign
-= // Subtract and assign
*= // Multiply and assign
/= // Divide and assign
%= // Modulus and assign

As an example:

let x = 5;
x += 3; // equivalent to x = x + 3
console.log(x); // 8

Increment and Decrement Operators

Increment and decrement operators are used to increase or decrease the value of a variable by 1.

++  // Increment
-- // Decrement

As an example:

let x = 5;
x++; // equivalent to x = x + 1
console.log(x); // 6

Conditional Operator (ternary operator)

The conditional operator (also called the ternary operator) is a shorthand way of writing an if-else statement.

condition ? expression1 : expression2;

As an example:

let x = 5;
let y = x > 3 ? "greater than 3" : "less than or equal to 3";
console.log(y); // 'greater than 3'

Bitwise Operators

Bitwise operators perform operations on the binary representation of numeric values.

&   // Bitwise AND
| // Bitwise OR
^ // Bitwise XOR
~ // Bitwise NOT
<< // Bitwise left shift
>> // Bitwise right shift
>>> // Zero-fill right shift

As an example:

let x = 5; // binary: 00000101
let y = 3; // binary: 00000011
console.log(x & y); // 1 (binary: 00000001)
console.log(x | y); // 7 (binary: 00000111)
console.log(~x); // -6 (binary: 11111010)
note

Understanding how to use operators is crucial to writing JavaScript code that performs the necessary operations on data.