Welcome to Day 3 of learning JavaScript! Today, we’ll explore operators and expressions—essential tools for performing calculations, making decisions, and writing meaningful logic in your programs.
Operators are special symbols or keywords that perform operations on values or variables. These operations can range from arithmetic calculations to logical decisions.
Used for mathematical operations like addition, subtraction, multiplication, etc.
|
Description |
Example |
let num1 = 10; let num2 = 3; console.log(num1 + num2); // 13 console.log(num1 % num2); // 1 Copy after login Copy after login Copy after login |
||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Addition | 5 3 | 8 | |||||||||||||||||||||||||||||
- | Subtraction | 10 - 6 | 4 | ||||||||||||||||||||||||||||
* | Multiplication | 4 * 2 | 8 | ||||||||||||||||||||||||||||
/ | Division | 12 / 4 | 3 | ||||||||||||||||||||||||||||
% | Modulus (Remainder) | 10 % 3 | 1 | ||||||||||||||||||||||||||||
** | Exponentiation | 2 ** 3 | 8 |
Operator | Description | Example | Output |
---|---|---|---|
== | Equal to | 5 == "5" | true |
=== | Strict equal to | 5 === "5" | false |
!= | Not equal to | 5 != "5" | false |
!== | Strict not equal | 5 !== "5" | true |
< | Less than | 5 < 10 | true |
> | Greater than | 10 > 5 | true |
<= | Less than or equal | 5 <= 5 | true |
>= | Greater than or equal | 10 >= 5 | true |
Operator | Description | Example | Output |
---|---|---|---|
== | Equal to | 5 == "5" | true |
=== | Strict equal to | 5 === "5" | false |
!= | Not equal to | 5 != "5" | false |
!== | Strict not equal | 5 !== "5" | true |
< | Less than | 5 < 10 | true |
> | Greater than | 10 > 5 | true |
<= | Less than or equal | 5 <= 5 | true |
>= | Greater than or equal | 10 >= 5 | true |
let num1 = 10; let num2 = 3; console.log(num1 + num2); // 13 console.log(num1 % num2); // 1
Combine multiple conditions or invert logic.
|
Description |
Example |
let age = 20; console.log(age >= 18); // true console.log(age === "20"); // false Copy after login Copy after login |
||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
&& | Logical AND | true && false | false | ||||||||||||||||
` | ` | Logical OR | |||||||||||||||||
! | Logical NOT | !true | false |
Operator | Description | Example | Output |
---|---|---|---|
= | Assign | x = 10 | 10 |
= | Add and assign | x = 5 | x = x 5 |
-= | Subtract and assign | x -= 3 | x = x - 3 |
*= | Multiply and assign | x *= 2 | x = x * 2 |
/= | Divide and assign | x /= 2 | x = x / 2 |
%= | Modulus and assign | x %= 3 | x = x % 3 |
let isAdult = true; let hasID = false; console.log(isAdult && hasID); // false console.log(isAdult || hasID); // true
Operator |
Description | Example | Output |
---|---|---|---|
= | Assign | x = 10 | 10 |
= | Add and assign | x = 5 | x = x 5 |
-= | Subtract and assign | x -= 3 | x = x - 3 |
*= | Multiply and assign | x *= 2 | x = x * 2 |
/= | Divide and assign | x /= 2 | x = x / 2 |
%= | Modulus and assign | x %= 3 | x = x % 3 |
Operator | Description | Precedence |
---|---|---|
** | Exponentiation | 1 |
*, /, % | Multiplication, Division, Modulus | 2 |
, - | Addition, Subtraction | 3 |
<, >, ==, != | Comparison | 4 |
&& | Logical AND | 5 |
` | ` |
Operator | Description | Precedence |
---|---|---|
** | Exponentiation | 1 |
*, /, % | Multiplication, Division, Modulus | 2 |
, - | Addition, Subtraction | 3 |
<, >, ==, != | Comparison | 4 |
&& | Logical AND | 5 |
` | ` |
let num1 = 10; let num2 = 3; console.log(num1 + num2); // 13 console.log(num1 % num2); // 1
Use parentheses () to control precedence.
let age = 20; console.log(age >= 18); // true console.log(age === "20"); // false
Today, we covered:
In Day 4, we’ll learn about Control Flow in JavaScript, focusing on conditional statements and loops. Stay tuned for Dec 11, 2024!
The above is the detailed content of Operators and Expressions in JavaScript. For more information, please follow other related articles on the PHP Chinese website!