Home >Web Front-end >JS Tutorial >Operators and Expressions in JavaScript

Operators and Expressions in JavaScript

Patricia Arquette
Patricia ArquetteOriginal
2024-12-14 21:49:15321browse

Operators and Expressions in JavaScript

Day 3: Operators and Expressions in JavaScript

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.


What Are Operators?

Operators are special symbols or keywords that perform operations on values or variables. These operations can range from arithmetic calculations to logical decisions.


Types of Operators in JavaScript

1. Arithmetic Operators

Used for mathematical operations like addition, subtraction, multiplication, etc.

Operator Description Example Output
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
let num1 = 10;
let num2 = 3;
console.log(num1 + num2); // 13
console.log(num1 % num2); // 1
Output
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

Example:

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
2. Relational (Comparison) Operators Used to compare two values and return a Boolean (true or false).
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

Example:

let num1 = 10;
let num2 = 3;
console.log(num1 + num2); // 13
console.log(num1 % num2); // 1

3. Logical Operators

Combine multiple conditions or invert logic.

Operator Description Example Output
&& Logical AND true && false false
` ` Logical OR
! Logical NOT !true false
Operator

Description

Example
let age = 20;
console.log(age >= 18); // true
console.log(age === "20"); // false
Output
&& Logical AND true && false false
` ` Logical OR
! Logical NOT !true false

Example:

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
4. Assignment Operators

Used to assign values to variables.

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

Example:

Operator Description Precedence
** Exponentiation 1
*, /, % Multiplication, Division, Modulus 2
, - Addition, Subtraction 3
<, >, ==, != Comparison 4
&& Logical AND 5
` `
Expressions and Precedence An expression is a piece of code that produces a value. For example, 5 3 is an expression, and its value is 8. Operator Precedence When multiple operators are used, JavaScript decides the order of execution based on precedence. Operators with higher precedence are executed first.
Operator Description Precedence
** Exponentiation 1
*, /, % Multiplication, Division, Modulus 2
, - Addition, Subtraction 3
<, >, ==, != Comparison 4
&& Logical AND 5
` `

Example of Precedence:

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

Practice for Today

  1. Write a program to calculate the area of a rectangle using arithmetic operators.
  2. Compare two numbers using relational operators and log whether they are equal or one is greater.
  3. Create a simple program to check if a person is eligible to vote using logical operators.

Summary of Day 3

Today, we covered:

  1. Arithmetic Operators: For calculations.
  2. Relational Operators: For comparisons.
  3. Logical Operators: For combining conditions.
  4. Assignment Operators: For assigning and updating values.
  5. Expressions and Precedence: Understanding execution order.

Next Steps

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn