🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEjavascript

javascript Documentation

LOADING ENGINE...

Operators

AI & DATA SCIENCE // operators

Operators perform operations on values. JS has arithmetic, comparison, logical, assignment, bitwise, and ternary operators.

Syntax

// Arithmetic: + - * / % **
// Comparison: === !== < > <= >=
// Logical: && || ! ??
// Assignment: = += -= *= /= **=
// Ternary: condition ? a : b

Deep Dive Course

Operators act on **operands** (values). JavaScript operators cover arithmetic, comparison (always prefer `===` over `==`), logical (`&&`, `||`, `??`), assignment, bitwise, and the ternary operator. The **nullish coalescing** `??` returns the right side only when the left is `null` or `undefined`.

1Understanding Operators

Operators act on operands (values). JavaScript operators cover arithmetic, comparison (always prefer === over ==), logical (&&, ||, ??), assignment, bitwise, and the ternary operator. The nullish coalescing ?? returns the right side only when the left is null or undefined.

💡

Always use === (strict equality) instead of == (loose equality) to avoid unexpected type coercion.

editor.html
// Arithmetic operators
console.log(10 + 3);  // 13
console.log(10 - 3);  // 7
console.log(10 * 3);  // 30
console.log(10 / 3);  // 3.333...
console.log(10 % 3);  // 1 (remainder)
console.log(2 ** 8);  // 256 (exponent)
localhost:3000

2Practical Example

Here is a real-world application of Operators showing how it is used in production JavaScript code.

editor.html
// Nullish coalescing vs logical OR
const count = 0;
console.log(count || 'default');  // 'default' (wrong!)
console.log(count ?? 'default');  // 0 (correct)

// Ternary
const age = 20;
const label = age >= 18 ? 'adult' : 'minor';
console.log(label); // 'adult'
localhost:3000

3Best Practices

Follow these guidelines when working with Operators:

1. Use === and !== for comparisons

2. Use ?? instead of || when 0 or '' are valid values

3. Use ** instead of Math.pow()

⚠️

Tip: Always use === (strict equality) instead of == (loose equality) to avoid unexpected type coercion.

editor.html
// Arithmetic operators
console.log(10 + 3);  // 13
console.log(10 - 3);  // 7
console.log(10 * 3);  // 30
console.log(10 / 3);  // 3.333...
console.log(10 % 3);  // 1 (remainder)
console.log(2 ** 8);  // 256 (exponent)
localhost:3000

Examples

Example 01Basic Usage
// Arithmetic operators
console.log(10 + 3);  // 13
console.log(10 - 3);  // 7
console.log(10 * 3);  // 30
console.log(10 / 3);  // 3.333...
console.log(10 % 3);  // 1 (remainder)
console.log(2 ** 8);  // 256 (exponent)
Example 02Advanced Example
// Nullish coalescing vs logical OR
const count = 0;
console.log(count || 'default');  // 'default' (wrong!)
console.log(count ?? 'default');  // 0 (correct)

// Ternary
const age = 20;
const label = age >= 18 ? 'adult' : 'minor';
console.log(label); // 'adult'

Best Practices

  • Use === and !== for comparisons
  • Use ?? instead of || when 0 or '' are valid values
  • Use ** instead of Math.pow()

Interview Question

What is the difference between == and ===?

Hint: One coerces types, the other doesn't.

== (loose equality) performs type coercion before comparing. === (strict equality) compares value AND type without coercion. Example: '5' == 5 is true, '5' === 5 is false. Always use === to avoid subtle bugs.

Exercises

MediumPractice using Operators in a real scenario.
View Solution
// Arithmetic operators
console.log(10 + 3);  // 13
console.log(10 - 3);  // 7
console.log(10 * 3);  // 30
console.log(10 / 3);  // 3.333...
console.log(10 % 3);  // 1 (remainder)
console.log(2 ** 8);  // 256 (exponent)

Frequently Asked Questions

What is the difference between == and ===?

== (loose equality) performs type coercion before comparing. === (strict equality) compares value AND type without coercion. Example: '5' == 5 is true, '5' === 5 is false. Always use === to avoid subtle bugs.

Related Functions

Data-TypesVariablesConditionals