Lesson 7 of 30 – JavaScript Operators
23%

JavaScript Operators

JavaScript operators perform operations on one or more operands and produce a result.

For example, in 1 + 2, the + symbol is an operator and returns the result 3.

Note: Operators are used for calculations, comparisons, assignments, logical decisions, and conditional expressions.
Types of JavaScript Operators

JavaScript includes the following categories of operators:

  • Arithmetic Operators
  • Comparison Operators
  • Logical Operators
  • Assignment Operators
  • Conditional Operators
  • Ternary Operator
Arithmetic Operators

Arithmetic operators perform mathematical calculations.

Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus (Remainder)
++ Increment
-- Decrement
Arithmetic Operator Example

Arithmetic operators perform calculations between numbers.

let x = 2, y = 5;

let z = x + y;
z = y - x;
z = x * y;
z = y / x;
z = y % x;

The operators return addition, subtraction, multiplication, division and remainder values.

Increment and Decrement Operators

The ++ and -- operators increase or decrease a variable by one.

Example:
let x = 6;

x++;
++x;
x--;
--x;
  • x++ : Post Increment
  • ++x : Pre Increment
  • x-- : Post Decrement
  • --x : Pre Decrement
String Concatenation

The + operator joins strings together.

Example:
let a = 5;
let b = "Hello ";
let c = "World!";
let d = 10;

a + b;
b + c;
a + d;
b + true;

If one operand is a string, the + operator performs concatenation.

Comparison Operators

Comparison operators compare two values and return either true or false.

Operator Description
== Equal to
=== Strict equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
Comparison Operator Example

Comparison operators compare values and return either true or false.

Example:
let a = 5, b = 10, c = "5";
let x = a;

a == c;
a === c;
a == x;
a != b;
a > b;
a < b;
a >= b;
a <= b;

The strict equality operator (===) compares both value and data type.

Logical Operators

Logical operators combine multiple conditions and return a boolean result.

Operator Description
&& Logical AND
|| Logical OR
! Logical NOT
Logical Operator Example

Logical operators are commonly used with conditions.

Example:
let a = 5, b = 10;

(a != b) && (a < b);
(a > b) || (a == b);
(a < b) || (a == b);
!(a < b);
!(a > b);
  • AND returns true when both conditions are true.
  • OR returns true if at least one condition is true.
  • NOT reverses the result.
Assignment Operators

Assignment operators assign values to variables and perform calculations.

Operator Description
= Assign value
+= Add and assign
-= Subtract and assign
*= Multiply and assign
/= Divide and assign
%= Modulus and assign
Assignment Operator Example

Assignment operators reduce the amount of code needed for calculations.

Example:
let x = 5;
let y = 10;

x = y;
x += 1;
x -= 1;
x *= 5;
x /= 5;
x %= 2;

These operators perform the calculation and assign the result back to the variable.

Operator Precedence

JavaScript follows mathematical precedence rules while evaluating expressions.

Example:
let result = 10 + 5 * 2;

Multiplication happens before addition, so the result is 20.

Use parentheses to control the order of evaluation.
Example:
let result = (10 + 5) * 2;

The result becomes 30.

Ternary Operator

JavaScript provides a special operator called the ternary operator (? :). It is a shorthand version of the if...else statement.

The syntax is:

condition ? value1 : value2;

If the condition is true, the first value is returned; otherwise, the second value is returned.

Ternary Operator Example
Example:
let a = 10;
let b = 5;

let c = a > b ? a : b;
let d = a > b ? b : a;

The variable c stores the larger value, while d stores the smaller value.

Operators Summary
  • Arithmetic operators perform calculations.
  • Comparison operators compare values.
  • Logical operators combine conditions.
  • Assignment operators assign values.
  • String concatenation joins text.
  • Increment and decrement change values by one.
  • Ternary operator provides a short form of if...else.
Best Practices
  • Use === instead of == whenever possible.
  • Use meaningful variable names.
  • Keep expressions simple and readable.
  • Use parentheses to improve clarity.
  • Avoid unnecessary nested ternary operators.
  • Use logical operators carefully.
  • Write clean and well-formatted code.

🧠 Quick Quiz

Which operator is used for strict equality in JavaScript?


s