JavaScript Operators Explained

Explore JavaScript operators and their precedence through practical examples.

🎯 Introduction

Operators are essential to JavaScript, whether you are performing arithmetic, checking conditions, or assigning values. Understanding them helps you write concise, efficient code with clear logic.

This article covers common arithmetic, comparison, logical, assignment, and conditional (ternary) operators, along with operator precedence and associativity. These concepts help you write more reliable code.


➕ Arithmetic Operators

Arithmetic operators perform calculations such as addition, subtraction, multiplication, and division. Common operators include:

OperatorMeaningExampleResult
+Addition3 + 47
-Subtraction10 - 64
*Multiplication5 * 840
/Division20 / 54
%Remainder9 % 41
++Increment (add 1)i++ or ++ii increases by 1
--Decrement (subtract 1)i-- or --ii decreases by 1

Example

var a = 5;
var b = 3;
var sum = a + b; // 8
var mod = a % b; // 2

var i = 10;
i++; // i becomes 11
--i; // i becomes 10 again
The ++ and -- operators have prefix and postfix forms. The prefix form updates the value before returning it; the postfix form returns the original value before updating it.

🔍 Comparison Operators

Comparison operators compare two values and return a boolean (true or false). Common operators include:

OperatorMeaningDescription
==EqualityCompares values with type conversion
===Strict equalityCompares values and types
!=InequalityChecks inequality with type conversion
!==Strict inequalityChecks for different values or types
>Greater thanLeft value is greater than right value
<Less thanLeft value is less than right value
>=Greater than or equalLeft value is greater than or equal to right value
<=Less than or equalLeft value is less than or equal to right value

Example

var x = 10;
var y = '10';

console.log(x == y); // true, because type conversion occurs
console.log(x === y); // false, because the types differ
console.log(x !== y); // true
console.log(x > 5); // true
Prefer === and !== to avoid errors caused by implicit type conversion.

🤔 Logical Operators

Logical operators combine boolean expressions. With boolean operands, they produce the following results:

OperatorMeaningDescription
&&Logical ANDReturns true only when both operands are true
||Logical ORReturns true when at least one operand is true
!Logical NOTReverses true to false and false to true

Example

var a = true;
var b = false;

console.log(a && b); // false
console.log(a || b); // true
console.log(!a); // false

Logical operators are often used in conditional statements and control flow.


📝 Assignment Operators

Assignment operators assign values to variables. Besides simple assignment with =, JavaScript provides compound assignment operators:

OperatorMeaningExampleDescription
=Assignmentx = 10Assigns a value directly
+=Addition assignmentx += 5Equivalent to x = x + 5
-=Subtraction assignmentx -= 3Equivalent to x = x - 3
*=Multiplication assignmentx *= 2Equivalent to x = x * 2
/=Division assignmentx /= 4Equivalent to x = x / 4
%=Remainder assignmentx %= 3Equivalent to x = x % 3

Example

var x = 10;
x += 5; // x = 15
x *= 2; // x = 30

❓ The Conditional (Ternary) Operator

This is JavaScript’s only operator that takes three operands. Its syntax is:

condition ? expression1 : expression2

If the condition is true, it returns expression1; otherwise, it returns expression2.

Example

var age = 20;
var status = age >= 18 ? 'Adult' : 'Minor';
console.log(status); // Adult

The ternary operator offers a concise alternative to a simple if-else statement.


⚖️ Operator Precedence and Associativity

Operators have different precedence levels: higher-precedence operators bind more tightly. Associativity determines how operators of the same precedence group from left to right or right to left.

OperatorDescriptionPrecedenceAssociativity
()Parentheses21Left to right ✅ Use for clarity
++, --Increment, decrement18Right to left
*, /, %Multiplication, division, remainder15Left to right
+, -Addition, subtraction14Left to right
<, <=, >, >=Comparison12Left to right
==, !=Equality, inequality11Left to right
&&Logical AND6Left to right
||Logical OR5Left to right
=, +=, etc.Assignment3Right to left
?:Conditional (ternary)4Right to left

Example

var a = 2 + 3 * 4; // 3 * 4 is evaluated first; result: 14
var b = (2 + 3) * 4; // Parentheses are evaluated first; result: 20
Use parentheses to make grouping explicit and your code easier to read.

❓ Knowledge Check

Q1: How do prefix and postfix ++ differ?

Prefix (++i) increments the variable and returns its new value. Postfix (i++) returns the original value and then increments the variable.

Q2: Why prefer === over ==?

=== does not convert types, helping avoid errors caused by implicit conversion.

Q3: How do && and || short-circuit?

With boolean operands, && skips the right operand when the left is false; || skips it when the left is true. This avoids unnecessary evaluation.


🧪 Exercises

Exercise 1: Arithmetic and Assignment

Alex has 10 units of money, earns 5 more (use +=), then loses 1 (use --). Write JavaScript to model these changes and print the remaining amount.

var money = 10;

// Write your code here

Exercise 2: Check Whether a Number Is Odd or Even

Use a ternary expression to determine whether num is odd or even and output the corresponding string.

var num = 7;

// Write your code here

alert(result); // Odd

🎉 You now understand JavaScript arithmetic, comparison, logical, assignment, and conditional operators, along with operator precedence. Keep practicing to strengthen your front-end programming skills!