- Courses
- 💻Programming Languages
- 🌐JavaScript
- Foundations
- Arithmetic & Logical Operators
On this page
JavaScript Basic Arithmetic and Logical Operators
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.
- Addition (
+): Adds two numbers or concatenates strings. - Subtraction (): Subtracts one number from another.
- Multiplication (): Multiplies two numbers.
- Division (
/): Divides the first number by the second. - Remainder/Modulo (
%): Returns the division remainder (useful for finding even/odd numbers). - Exponentiation (): Raises a number to the power of another.
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.
- Assignment (
=): Sets a variable's value. - Addition Assignment (
+=): Adds a value to a variable and updates it. - Subtraction Assignment (
=): Subtracts and updates. - Multiplication/Division Assignment (
=,/=): Multiplies or divides and updates.
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:
- Loose Equality (
==): Compares values only, performing "type coercion" (e.g.,'5' == 5istrue). - Strict Equality (
===): Compares both value and type. This is the best practice (e.g.,'5' === 5isfalse).
Other Comparisons
- Strict Inequality (
!==): Checks if values and types are NOT equal. - Greater Than (
>) / Less Than (<): Standard numeric comparisons. - Greater Than or Equal (
>=) / Less Than or Equal (<=): Checks for range boundaries.
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!
Previous
Primitive Data Types
Next
Control Flow
-
JavaScript Arithmetic Operators
-
Assignment Operators
-
Comparison Operators
- The Difference Between Equality Operators
- Other Comparisons
-
Summary Table: Operator Reference
