What are Operators in JavaScript

What are Operators in JavaScript?

Operators in JavaScript are symbols used to carry out operations on variables and values. They allow you to manipulate data, perform calculations, and make decisions within your code. Operators work with operands (values or variables) to produce a result.

Types of Operators in JavaScript

  1. Arithmetic Operators – Perform basic mathematical operations.
  2. Assignment Operators – Assign values to variables.
  3. Comparison Operators – Evaluate two values and return a Boolean result (true or false).
  4. Logical Operators – Carry out logical operations (AND, OR, NOT).
  5. String Operators – Concatenate (join) strings.
  6. Bitwise Operators – Carry out operations at the binary level.
  7. Ternary (Conditional) Operator – A shorthand for if-else conditions.
  8. Type Operators – Determine or manipulate the type of a value.
  9. Increment/Decrement Operators – Increase or decrease a value by 1.

How to Use Operators in JavaScript

Operators are used in expressions to perform calculations, compare values, and control program flow.

1. Arithmetic Operators

Perform mathematical calculations.

OperatorDescriptionExampleResult
+Addition5 + 38
-Subtraction10 - 64
*Multiplication4 * 28
/Division8 / 24
%Modulus (Remainder)10 % 31
**Exponentiation2 ** 38

Example:

let a = 10; let b = 3; console.log(a + b); // 13 console.log(a % b); // 1 (Remainder of 10 / 3)

2. Assignment Operators

Assign values to variables.

OperatorDescriptionExampleEquivalent To
=Assignx = 10x = 10
+=Add or assignx += 5x = x + 5
-=Subtract or assignx -= 3x = x - 3
*=Multiply or assignx *= 2x = x * 2
/=Divide or assignx /= 4x = x / 4
%=Modulus or assignx %= 2x = x % 2

Example:

let x = 5; x += 3; // x = 8 x *= 2; // x = 16 console.log(x);

3. Comparison Operators

Compare two values and return true or false.

OperatorDescriptionExampleResult
==Equal to5 == "5"true
===Strict equal (type + value)5 === "5"false
!=Not equal5 != 3true
!==Strict not equal5 !== "5"true
>Greater than10 > 5true
<Less than3 < 7true
>=Greater than or equal8 >= 8true
<=Less than or equal6 <= 5false

Example:

let age = 18; console.log(age >= 18); // true console.log(age === "18"); // false (different types)

4. Logical Operators

Combine multiple conditions.

OperatorDescriptionExampleResult
&&Logical ANDtrue && falsefalse
``Logical OR
!Logical NOT!truefalse

Example:

let isAdult = true; let hasID = false; console.log(isAdult && hasID); // false console.log(isAdult || hasID); // true

5. String Operators

Used to concatenate (join) strings.

let firstName = "Maher"; let lastName = "Baan"; let fullName = firstName + " " + lastName; // "Maher Baan"

6. Ternary Operator

A shorthand for if-else statements.

let age = 20; let canVote = (age >= 18) ? "Yes" : "No"; console.log(canVote); // Yes

7. Increment/Decrement Operators

Increase or decrease a variable by 1.

OperatorDescriptionExampleResult
++Incrementx++x + 1
--Decrementx--x - 1

Example:

let count = 5; count++; // 6 count--; // 5 console.log(count);

Why Use Operators?

  1. Perform Calculations – For mathematical operations.
  2. Make Decisions – Use comparison and logical operators to control program flow.
  3. Manipulate Data – Update, assign, and transform data easily.
  4. Shorter Code – Reduce the need for lengthy if-else statements.

When to Use Operators?

  • Arithmetic Operations – For performing calculations.
  • Conditional Checks – In if statements and loops.
  • String Concatenation – To build messages or form outputs dynamically.
  • Boolean Logic – To handle multiple conditions efficiently.

Example: Combining Operators

let price = 100; let discount = 0.1; let finalPrice = price - (price * discount); // Arithmetic let canBuy = (finalPrice < 80) ? "Buy it!" : "Too expensive"; // Ternary console.log(canBuy);

Post a Comment

Previous Post Next Post