🔢 JavaScript Operators & Expressions
Estimated reading: 3 minutes 268 views

JavaScript Operators: A Complete Guide to Manipulating Data and Logic

JavaScript operators are powerful tools that allow developers to manipulate data, perform calculations, and control program flow.
Ever been confused why 5 + "5" yields “55” instead of 10?
Or wondered why your conditional logic isn’t working as expected?

Understanding operators is foundational to writing effective JavaScript code. In this guide, you’ll learn:

The types of JavaScript operators
How to use them with examples
Best practices and real-world usage


Core Concepts and Theory

Operators in JavaScript are symbols that perform actions on values (operands).
They are the building blocks of all logic and data manipulation.


Types of JavaScript Operators

Category Examples
Arithmetic+, -, *, /, %, **
Assignment=, +=, -=, *=, /=, **=
Comparison==, ===, !=, !==, >, <
Logical&&, `
Bitwise&, `
String+, += (for concatenation)
❔ Conditional (Ternary)condition ? true : false
Unarytypeof, void, !, ++, --
Typetypeof, instanceof

Operator Precedence (Execution Order)

PrecedenceCategoryExamples
HighestGrouping( )
Member Accessobj.prop, arr[index]
Function CallsmyFunc()
Unary!, typeof, ++, --
Arithmetic**, *, /, %, +, -
Comparison==, ===, !=, !==, <, >
Logical&&, `
Conditional?:
LowestAssignment=, +=, *= etc.

Code Implementation & Examples

Arithmetic Operators

function W3OfficeArithmeticDemo() {
let a = 10, b = 3;
console.log(a + b); // 13
console.log(a - b); // 7
console.log(a * b); // ✖️ 30
console.log(a / b); // 3.333...
console.log(a % b); // 1
console.log(a ** b); // 1000
}
W3OfficeArithmeticDemo();

Explanation:

  • + adds numbers, * multiplies, % gets the remainder, ** performs exponentiation.

Assignment Operators

function W3OfficeAssignmentDemo() {
let total = 100;
total += 50; // = 150
total -= 25; // = 125
total *= 2; // ✖️= 250
total /= 5; // ➗= 50
total %= 30; // = 20
total **= 2; // = 400
console.log(`Final Total: ${total}`);
}
W3OfficeAssignmentDemo();

Use Case: Updating totals in a cart, scoring systems, or numeric counters.


Comparison Operators

function W3OfficeComparisonDemo() {
let a = 5, b = '5', c = 10;
console.log(a == b); // true (loose equality)
console.log(a === b); // false (strict equality)
console.log(a != c); // true
console.log(a !== b); // true
console.log(c > a); // true
console.log(a <= 5); // true
}
W3OfficeComparisonDemo();

Use Case: Validation forms, access checks, filtering data.


Logical Operators

function W3OfficeLogicalDemo() {
let isLoggedIn = true;
let isAdmin = false;

console.log(isLoggedIn && isAdmin); // false
console.log(isLoggedIn || isAdmin); // true
console.log(!isLoggedIn); // false
}
W3OfficeLogicalDemo();

Real-world Use:
Control access levels, toggle UI visibility, form conditionals.


❔ Ternary Operator

function W3OfficeTernaryDemo() {
let age = 20;
let drink = age >= 21 ? "Beer" : " Juice";
console.log(drink); // Output: Juice
}
W3OfficeTernaryDemo();

Ternary Tip: Useful for inline decisions like rendering UI elements or quick checks.


Bitwise Operators

function W3OfficeBitwiseDemo() {
let a = 5; // 0101
let b = 3; // 0011
console.log(a & b); // 0001 => 1
console.log(a | b); // 0111 => 7
console.log(a ^ b); // 0110 => 6
console.log(~a); // 1010 => -6
console.log(a << 1); // 1010 => 10
}
W3OfficeBitwiseDemo();

Note: Rarely used unless working with low-level operations like graphics or compression.


Spread & Rest Operators

function W3OfficeSpreadRestDemo() {
const nums = [1, 2, 3];
const moreNums = [...nums, 4, 5]; // Spread [1,2,3,4,5]
console.log(moreNums);

function sum(...args) {
return args.reduce((acc, curr) => acc + curr, 0);
}
console.log(sum(1, 2, 3)); // Rest 6
}
W3OfficeSpreadRestDemo();

Common Use:
Merging arrays, cloning objects, collecting arguments in functions.


Summary & Key Takeaways

JavaScript operators are core tools for any developer
Learn and apply arithmetic, assignment, and logical operators confidently
Use comparison for decision making
Apply ternary and logical short-circuiting for concise expressions
Use spread/rest for modern clean code


FAQ Section

What’s the difference between == and ===?

== compares values after type conversion; === checks both value and type.

When to use ?? vs ||?

Use ?? to default only on null/undefined; use || to default on any falsy value.

Why do bitwise operations convert to 32-bit integers?

JavaScript converts numbers to 32-bit for bitwise logic to improve performance consistency.

How does short-circuit logic work?

With && or ||, evaluation stops early if the result is already known — useful in defaults and conditionals.

Can I chain ternary operations?

Yes, but use sparingly. Nested ternaries can get confusing—prefer if/else for readability.


Share Now :
Share

JavaScript Operators

Or Copy Link

CONTENTS
Scroll to Top