Skip to main content

JavaScript Operators

Operators in JavaScript are symbols or keywords that perform operations on one or more operands. They are instrumental in manipulating values and variables within expressions. Let's explore some of the key operators in JavaScript.

TypeOperatorDescriptionExample
Arithmetic+Addition2 + 3 = 5
-Subtraction5 - 2 = 3
*Multiplication2 * 4 = 8
/Division10 / 2 = 5
%Modulus (remainder)8 % 3 = 2
**Exponentiation2 ** 3 = 8
Assignment=Assignmentx = 10
+=Add and assignx += 5 (equivalent to x = x + 5)
-=Subtract and assigny -= 3 (equivalent to y = y - 3)
*=Multiply and assignz *= 2 (equivalent to z = z * 2)
/=Divide and assigna /= 4 (equivalent to a = a / 4)
%=Modulus and assignb %= 7 (equivalent to b = b % 7)
Comparison==Loose equality (checks value, not type)10 == "10" (true)
===Strict equality (checks both value and type)10 === "10" (false)
!=Loose inequality5 != 7 (true)
!==Strict inequality"5" !== 7 (true)
<Less than2 < 5 (true)
>Greater than8 > 3 (true)
<=Less than or equal to4 <= 4 (true)
>=Greater than or equal to1 >= 0 (true)
Logical&&Logical AND (both operands must be truthy)true && false = false
``
!Logical NOT (inverts the operand)!true = false
Increment/Decrement++Increment (adds 1)x++ (increment after) or ++x (increment before)
--Decrement (subtracts 1)y-- (decrement after) or --y (decrement before)
Ternary? :Conditional expressionage >= 18 ? "adult" : "minor"
TypeoftypeofReturns the type of a valuetypeof 10 = "number"
IninChecks if a property exists in an object"name" in person
DeletedeleteDeletes a property from an objectdelete person.age

Arithmetic Operators:

Arithmetic operators perform basic mathematical operations.

  • Addition (+):

    let sum = 5 + 3; // Result: 8
  • Subtraction (-):

    let difference = 10 - 4; // Result: 6
  • Multiplication (*):

    let product = 7 * 2; // Result: 14
  • Division (/):

    let quotient = 20 / 5; // Result: 4
  • Modulus (%):

    let remainder = 15 % 4; // Result: 3

Assignment Operator (=):

The assignment operator assigns a value to a variable.

let age = 25;

Comparison Operators:

Comparison operators are used to compare values, resulting in a Boolean outcome (true or false).

  • Equal to (==):

    let isEqual = 10 == 10; // Result: true
  • Strictly equal to (===):

    let isStrictlyEqual = "5" === 5; // Result: false
  • Not equal to (!=):

    let isNotEqual = 8 != 12; // Result: true
  • Greater than (>):

    let isGreater = 15 > 10; // Result: true
  • Less than (<):

    let isLess = 5 < 2; // Result: false

Logical Operators:

Logical operators perform logical operations and return Boolean values.

  • Logical AND (&&):

    let logicalAnd = true && false; // Result: false
  • Logical OR (||):

    let logicalOr = true || false; // Result: true
  • Logical NOT (!):

    let logicalNot = !true; // Result: false

Increment (++) and Decrement (--) Operators:

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

let count = 5;
count++; // Increment: count is now 6
count--; // Decrement: count is now 5

Concatenation Operator (+):

The + operator is also used for string concatenation.

let greeting = "Hello, " + "World!"; // Result: "Hello, World!"

These are some fundamental operators in JavaScript. Understanding how to use them is essential for writing effective and expressive code. In upcoming chapters, we'll delve into more advanced operators and their applications.