Lesson 4: JavaScript Operators

Learning Objectives

By the end of this lesson, learners will:

  • Understand what an operator is in programming
  • Learn the different types of JavaScript operators
  • Use arithmetic, assignment, comparison, and logical operators in code
  • Know the difference between == and ===
  • Write expressions that produce dynamic results

What Is an Operator?

In JavaScript, an operator is a symbol that performs an operation on one or more values (called operands). It tells the computer to do something, like add numbers, compare values, or assign variables.

πŸ“Œ Example:

let total = 5 + 3; // "+" is an operator

1. Arithmetic Operators

Used to perform basic mathematical calculations.

OperatorSymbolExampleResult
Addition+5 + 27
Subtraction-10 - 46
Multiplication*3 * 39
Division/8 / 24
Modulus (remainder)%10 % 31
Exponentiation**2 ** 38

πŸ“Œ Example:

let a = 10;
let b = 3;

console.log(a % b); // 1 (remainder)
console.log(2 ** 4); // 16 (2 raised to the 4th power)

2. Assignment Operators

Used to assign values to variables.

OperatorExampleMeaning
=x = 5Assign 5 to x
+=x += 2Add 2 to x
-=x -= 1Subtract 1 from x
*=x *= 3Multiply x by 3
/=x /= 2Divide x by 2
%=x %= 4x = x mod 4

πŸ“Œ Example:

let x = 10;
x += 5; // same as x = x + 5
console.log(x); // 15

3. Comparison Operators

Used to compare values. These return Boolean results: true or false.

OperatorDescriptionExampleResult
==Equal (loose)5 == "5"true
===Equal (strict)5 === "5"false
!=Not equal (loose)5 != "5"false
!==Not equal (strict)5 !== "5"true
>Greater than8 > 5true
<Less than3 < 4true
>=Greater or equal10 >= 10true
<=Less or equal6 <= 3false

== vs ===: What’s the Difference?

  • == checks only value and does type coercion
    β†’ 5 == "5" β†’ true
  • === checks value and type (strict)
    β†’ 5 === "5" β†’ false

βœ… Always use === in modern JavaScript to avoid unexpected results.


4. Logical Operators

Used to combine or invert Boolean expressions.

OperatorNameExampleResult
&&ANDtrue && truetrue
``OR
!NOT!truefalse

πŸ“Œ Examples:

let isLoggedIn = true;
let isAdmin = false;

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

5. String Concatenation with +

The + operator is also used to combine strings.

πŸ“Œ Example:

let firstName = "Alice";
let lastName = "Smith";
let fullName = firstName + " " + lastName;

console.log(fullName); // Alice Smith

🧠 If one operand is a string, + will treat the other as a string too.


6. Type Coercion

JavaScript tries to convert values automatically when needed.

πŸ“Œ Example:

console.log("5" + 1); // "51" β†’ number is converted to string
console.log("5" - 1); // 4 β†’ string is converted to number

βœ… Be careful β€” always use strict operators (===) to avoid confusion.


Practice Exercise

Try this in your browser console:

let a = 10;
let b = 5;

console.log(a + b);     // 15
console.log(a * b);     // 50
console.log(a == "10"); // true
console.log(a === "10"); // false
console.log(a > b && b < 3); // false

Summary Table

TypeOperatorsUse
Arithmetic+, -, *, /, %, **Calculations
Assignment=, +=, -=, etc.Assign/change values
Comparison==, ===, !=, >, etc.Compare values
Logical&&, `
String+Combine (concatenate) text