Chapter 3_ Operators and Expressions in JavaScript
Chapter 3_ Operators and Expressions in JavaScript
By Ahmed Thaer
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.
+ 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.
= 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).
Tip: I always use === and !== for comparisons to avoid surprises with type conversion.
Logical Operators
` ` OR
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
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
Quick Practice
2. Combine your first and last name into one string variable.
Summary
In this chapter, I covered:
Next up: Control Structures—how to make decisions in your code using if statements and
loops!