On this page

JavaScript Basic Arithmetic and Logical Operators

Enrico Hochkoeppler Profile

By Enrico Hochkoeppler

Web Developer & TutoriaLibre Founder

In JavaScript programming, operators are symbols used to perform operations on operands (variables and values). We categorize these into three primary groups: Arithmetic, Assignment, and Comparison.

JavaScript Arithmetic Operators​

These are the standard mathematical operators used to perform calculations.

Code Example:


let sum = 10 + 5; // 15

let product = 4 * 2; // 8

let remainder = 10 % 3; // 1 (10 divided by 3 is 3 with 1 left over)

let power = 2 ** 3; // 8 (2 to the power of 3)

Assignment Operators​

Assignment operators are used to assign values to JavaScript variables. While the basic assignment is =, there are "compound" versions that perform an operation and assign the result simultaneously.

Code Example:


let score = 10;

score += 5; // Equivalent to: score = score + 5 (Result: 15)

score *= 2; // Equivalent to: score = score * 2 (Result: 30)

Comparison Operators​

To master JavaScript logic, you must understand how to compare two values. Comparison operators always return a Boolean (true or false).

The Difference Between Equality Operators​

One of the most important concepts for a Junior Developer is the difference between "loose" and "strict" equality:

Other Comparisons​

Code Example:


let x = 10;

let y = "10";



console.log(x == y); // true (loose equality)

console.log(x === y); // false (strict equality - Recommended!)

console.log(x > 5); // true

Summary Table: Operator Reference​

CategoryOperatorDescriptionExampleArithmetic+, -, *, /Standard Math10 / 2 = 5Modulo%Remainder of division7 % 2 = 1Assignment=Assigns a valuelet a = 5Compound+=, -=Operation + Assignmenta += 2 (is 7)Strict Equal===Value & Type check5 === 5 (true)Relational>, <Greater/Less than10 > 2 (true)


Key Takeaway: Always default to Strict Equality (===). Using loose equality (==) can lead to unexpected bugs caused by JavaScript's automatic type conversion. Mastering these operators is essential for the next lesson on Control Flow and Logic.

Found this helpful? Share the knowledge...

... or support this project to keep it alive!

Buy Me a Coffee

Previous

Primitive Data Types

Next

Control Flow