2.1K Views

JS104 - Operators & Expressions

Learn how operators work in JavaScript including arithmetic, comparison, logical operators, and how expressions are evaluated.

Operators allow you to perform calculations, compare values, and build logic in your JavaScript programs.
An expression is any piece of code that produces a value.

Arithmetic Operators

Arithmetic operators are used for basic mathematical operations.

let a = 10;
let b = 3;
 
a + b;   // 13
a - b;   // 7
a * b;   // 30
a / b;   // 3.333...
a % b;  // 1 (remainder)
a ** b; // 1000 (exponent)

Assignment Operators

let x = 5;
x += 3; // 8
x -= 2; // 6
x *= 2; // 12

Shorter syntax makes code easier to maintain.


Comparison Operators

Used to compare two values.

5 > 3;   // true
5 < 3;   // false
5 >= 5;  // true
5 <= 4;  // false

Equality

5 == "5"   // true (loose equality)
5 === "5" // false (strict equality)

Always prefer strict equality (===).

Inequality

10 !== 10 // false
10 !== 5  // true

Logical Operators

Used for combining conditions.

true && true   // true
true && false  // false
true || false  // true
false || false // false
!true           // false

Logical operators are essential for conditional logic.


String Operators

let first = "Hello";
let second = "World";
 
first + " " + second; // "Hello World"

Expressions in JavaScript

An expression produces a value.

Examples:

2 + 2
"JS" + "101"
isLoggedIn && hasToken

All of these generate a result and can be assigned to variables.


Operator Precedence

Some operators run before others.
Multiplication happens before addition.

2 + 3 * 4 // 14 (not 20)

Use parentheses to control execution order:

(2 + 3) * 4 // 20

Summary

In this lesson, you learned:

  • Arithmetic operators
  • Assignment operators
  • Comparison and equality operators
  • Logical operators
  • String concatenation
  • How expressions produce values
  • Operator precedence

Next, we will introduce control flow and learn how JavaScript makes decisions using if, else, switch and the ternary operator.