Chapter 3: Operators and Expressions in JavaScript
By Ahmed Thaer
What Are Operators and Expressions?
After learning about variables and data types, I wanted to do something with them—like add
numbers, combine text, or check if something is true. That’s where operators come in.
Operators let you perform actions with your variables. When you use operators with values or
variables, you create what’s called an expression.
Think of an expression as a little statement that gives you a value, like 2 + 2 or 'Hello, ' +
name.
Arithmetic Operators
These are the classic math operators. I use them whenever I want to add, subtract, multiply, or
divide numbers.
Operator Description Example Result
+ Addition 5 + 3 8
- Subtraction 5 - 3 2
* Multiplication 5 * 3 15
/ Division 6 / 3 2
% Modulus (Remainder) 5 % 2 1
** Exponentiation 2 ** 3 8
Example:
javascript
CopyEdit
let a = 10;
let b = 3;
console.log(a + b); // 13
console.log(a % b); // 1
console.log(a ** b); // 1000
Assignment Operators
Assignment operators help you give a value to a variable or update its value.
Operator Example Same As
= x = 5 assign 5 to
x
+= x += 2 x = x + 2
-= x -= 2 x = x - 2
*= x *= 2 x = x * 2
/= x /= 2 x = x / 2
Example:
javascript
CopyEdit
let count = 5;
count += 3; // count is now 8
count *= 2; // count is now 16
String Operators
The + operator is also used to combine strings (text) in JavaScript—a process called
“concatenation.”
javascript
CopyEdit
let firstName = 'Ahmed';
let greeting = 'Hello, ' + firstName + '!';
console.log(greeting); // Hello, Ahmed!
Comparison Operators
Comparison operators let you compare values, and the result is always true or false (a
Boolean).
Operator Description Example Result
== Equal to (loose) 5 == true
'5'
=== Equal to (strict) 5 === false
'5'
!= Not equal (loose) 5 != true
'8'
!== Not equal (strict) 5 !== true
'5'
> Greater than 7 > 5 true
< Less than 7 < 5 false
>= Greater or equal 5 >= 5 true
<= Less or equal 4 <= 4 true
Tip: I always use === and !== for comparisons to avoid surprises with type conversion.
Logical Operators
When I want to combine multiple conditions, I use logical operators:
Operator Description Example Result
&& AND true && false
false
` ` OR
! NOT (negation) !true false
Example:
javascript
CopyEdit
let isAdult = true;
let hasTicket = false;
console.log(isAdult && hasTicket); // false (both must be true)
console.log(isAdult || hasTicket); // true (at least one is true)
console.log(!isAdult); // false
Expressions in Action
Let’s combine what we’ve learned:
javascript
CopyEdit
let x = 7;
let y = 4;
let sum = x + y; // 11
let message = 'The total is ' + sum;
console.log(message); // The total is 11
let canEnter = (x > 5) && (y < 10);
console.log(canEnter); // true
Quick Practice
Try these in your browser console:
1. What’s the result of 10 % 3?
2. Combine your first and last name into one string variable.
3. Check if 15 >= 10 && 5 < 3 is true or false.
Summary
In this chapter, I covered:
● Arithmetic, assignment, string, comparison, and logical operators
● How to use them to build expressions
● Why strict comparison (===) is usually safer
Next up: Control Structures—how to make decisions in your code using if statements and
loops!