0% found this document useful (0 votes)
12 views5 pages

JavaScript Operators and Expressions

Chapter 3 discusses operators and expressions in JavaScript, explaining how operators allow for mathematical operations, variable assignments, string concatenation, value comparisons, and logical conditions. It covers various types of operators including arithmetic, assignment, comparison, and logical operators, providing examples for each. The chapter emphasizes the importance of using strict comparison operators for safer code and concludes with practice exercises for readers.

Uploaded by

Ahmed Salah
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views5 pages

JavaScript Operators and Expressions

Chapter 3 discusses operators and expressions in JavaScript, explaining how operators allow for mathematical operations, variable assignments, string concatenation, value comparisons, and logical conditions. It covers various types of operators including arithmetic, assignment, comparison, and logical operators, providing examples for each. The chapter emphasizes the importance of using strict comparison operators for safer code and concludes with practice exercises for readers.

Uploaded by

Ahmed Salah
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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;

[Link](a + b); // 13
[Link](a % b); // 1
[Link](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 + '!';
[Link](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;
[Link](isAdult && hasTicket); // false (both must be true)
[Link](isAdult || hasTicket); // true (at least one is true)
[Link](!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;
[Link](message); // The total is 11

let canEnter = (x > 5) && (y < 10);


[Link](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!

Common questions

Powered by AI

Assignment operators simplify JavaScript syntax by reducing redundancy. Instead of writing x = x + 2, the shorthand x += 2 achieves the same result without repeating the variable name. This results in cleaner, more readable code by streamlining operations .

Expressions in JavaScript can combine different operators; for instance, let x = 7 and y = 4 are used in sum = x + y to calculate 11. This value can be concatenated in a string 'The total is ' + sum to produce 'The total is 11'. Logical operators can further assess conditions, such as (x > 5) && (y < 10) resulting in true .

The string operator + is primarily used for concatenation, which combines text strings. For example, 'Hello, ' + 'Ahmed' results in 'Hello, Ahmed'. This capability allows developers to build dynamic strings by combining multiple string variables and literals .

Logical operators such as AND (&&), OR (||), and NOT (!) are used to combine or invert boolean conditions in an expression. For example, (true && false) evaluates to false because both conditions must be true, while (true || false) evaluates to true since at least one condition is true .

Using strict comparison operators like === and !== is significant because they prevent unexpected results due to type coercion. These operators compare both value and type, ensuring more predictable and accurate comparisons, unlike loose equality which may convert types .

Comparison operators are used to compare values, resulting in a Boolean value of true or false. Operators like == (equal to) and === (strict equality) determine if two values are the same, with the strict version additionally checking type, avoiding automatic type conversion issues .

Assignment operators like =, +=, -=, *=, and /= update the values of variables. For instance, count += 3 increments the current value of count by 3, while count *= 2 doubles the current value of count .

The expression 5 % 2 results in 1, which is the remainder of dividing 5 by 2. The modulus operator is useful for determining if a number is even or odd, among other applications in cycles and patterns .

Arithmetic operators such as addition (+), subtraction (-), multiplication (*), and division (/) are used in JavaScript to perform calculations between numbers. For example, the expression 5 + 3 results in 8, while 6 / 3 results in 2 .

Logical operators are crucial in conditional statements for specifying complex conditions where multiple sub-conditions need to be met. AND (&&) ensures that all defined conditions are true, OR (||) verifies if at least one condition holds, and NOT (!) reverses the state of a condition. These allow for comprehensive condition handling, essential for robust decision-making in code .

You might also like