Hướng dẫn types of operators in javascript with example - các loại toán tử trong javascript với ví dụ

JavaScript bao gồm các toán tử giống như các ngôn ngữ khác. Một toán tử thực hiện một số hoạt động trên một hoặc nhiều toán hạng (giá trị dữ liệu) và tạo ra kết quả. Ví dụ, trong 1 + 2, dấu + là một toán tử và 1 là toán hạng bên trái và 2 là toán hạng bên phải. Toán tử + thực hiện bổ sung hai giá trị số và trả về kết quả.

Show

JavaScript bao gồm các loại nhà khai thác sau đây.

  1. Toán tử số học
  2. Toán tử so sánh
  3. Toán tử logic
  4. Toán tử chuyển nhượng
  5. Toán tử có điều kiện
  6. Toán tử ternary

Toán tử số học

Các toán tử số học được sử dụng để thực hiện các hoạt động toán học giữa các toán hạng số.

Nhà điều hành Sự mô tả
+ Thêm hai toán hạng số.
- Trừ toán hạng bên phải từ toán hạng bên trái
* Nhân hai toán hạng số.
/ Chia hạng bên trái cho toán hạng bên phải.
Phần trăm Toán tử mô đun. Trả lại phần còn lại của hai toán hạng.
++ Toán tử gia tăng. Tăng giá trị toán hạng lên một.
- Toán tử giảm. Giảm giá trị của một.

Ví dụ sau đây cho thấy cách các toán tử số học thực hiện các tác vụ khác nhau trên toán hạng.

let x = 5, y = 10;

let z = x + y; //performs addition and returns 15

z = y - x; //performs subtraction and returns 5

z = x * y; //performs multiplication and returns 50

z = y / x; //performs division and returns 2

z = x % 2; //returns division remainder 1

Các toán tử

let x = 5;

x++; //post-increment, x will be 5 here and 6 in the next line

++x; //pre-increment, x will be 7 here  

x--; //post-decrement, x will be 7 here and 6 in the next line

--x; //pre-decrement, x will be 5 here
1 và
let x = 5;

x++; //post-increment, x will be 5 here and 6 in the next line

++x; //pre-increment, x will be 7 here  

x--; //post-decrement, x will be 7 here and 6 in the next line

--x; //pre-decrement, x will be 5 here
2 là các toán tử đơn. Nó chỉ hoạt động với toán hạng bên trái hoặc phải. Khi được sử dụng với toán hạng bên trái, ví dụ:
let x = 5;

x++; //post-increment, x will be 5 here and 6 in the next line

++x; //pre-increment, x will be 7 here  

x--; //post-decrement, x will be 7 here and 6 in the next line

--x; //pre-decrement, x will be 5 here
3, nó sẽ tăng giá trị của
let x = 5;

x++; //post-increment, x will be 5 here and 6 in the next line

++x; //pre-increment, x will be 7 here  

x--; //post-decrement, x will be 7 here and 6 in the next line

--x; //pre-decrement, x will be 5 here
4 khi kiểm soát chương trình chuyển sang câu lệnh tiếp theo. Theo cách tương tự, khi nó được sử dụng với toán hạng bên phải, ví dụ,
let x = 5;

x++; //post-increment, x will be 5 here and 6 in the next line

++x; //pre-increment, x will be 7 here  

x--; //post-decrement, x will be 7 here and 6 in the next line

--x; //pre-decrement, x will be 5 here
5, nó sẽ tăng giá trị của
let x = 5;

x++; //post-increment, x will be 5 here and 6 in the next line

++x; //pre-increment, x will be 7 here  

x--; //post-decrement, x will be 7 here and 6 in the next line

--x; //pre-decrement, x will be 5 here
4 chỉ có. Do đó,
let x = 5;

x++; //post-increment, x will be 5 here and 6 in the next line

++x; //pre-increment, x will be 7 here  

x--; //post-decrement, x will be 7 here and 6 in the next line

--x; //pre-decrement, x will be 5 here
3 được gọi là sau khi tăng cường và
let x = 5;

x++; //post-increment, x will be 5 here and 6 in the next line

++x; //pre-increment, x will be 7 here  

x--; //post-decrement, x will be 7 here and 6 in the next line

--x; //pre-decrement, x will be 5 here
5 được gọi là trước khi tăng.

let x = 5;

x++; //post-increment, x will be 5 here and 6 in the next line

++x; //pre-increment, x will be 7 here  

x--; //post-decrement, x will be 7 here and 6 in the next line

--x; //pre-decrement, x will be 5 here

Kết hợp chuỗi

Toán tử + thực hiện thao tác nối khi một trong các toán hạng thuộc loại chuỗi. Ví dụ sau đây cho thấy sự kết hợp chuỗi ngay cả khi một trong các toán hạng là một chuỗi.

let a = 5, b = "Hello ", c = "World!", d = 10;

a + b; //returns "5Hello "

b + c; //returns "Hello World!"

a + d; //returns 15

b + true; //returns "Hello true"

c - b; //returns NaN; - operator can only used with numbers

Toán tử so sánh

JavaScript cung cấp các toán tử so sánh so sánh hai toán hạng và trả về giá trị boolean

let a = 5, b = "Hello ", c = "World!", d = 10;

a + b; //returns "5Hello "

b + c; //returns "Hello World!"

a + d; //returns 15

b + true; //returns "Hello true"

c - b; //returns NaN; - operator can only used with numbers
0 hoặc
let a = 5, b = "Hello ", c = "World!", d = 10;

a + b; //returns "5Hello "

b + c; //returns "Hello World!"

a + d; //returns 15

b + true; //returns "Hello true"

c - b; //returns NaN; - operator can only used with numbers
1.

Người vận hành Sự mô tả
+ Thêm hai toán hạng số.
- Trừ toán hạng bên phải từ toán hạng bên trái
* Nhân hai toán hạng số.
/ Chia hạng bên trái cho toán hạng bên phải.
Phần trăm Toán tử mô đun. Trả lại phần còn lại của hai toán hạng.
++ Toán tử gia tăng. Tăng giá trị toán hạng lên một.
- Toán tử giảm. Giảm giá trị của một.

Ví dụ sau đây cho thấy cách các toán tử số học thực hiện các tác vụ khác nhau trên toán hạng.

let a = 5, b = 10, c = "5";
let x = a;

a == c; // returns true

a === c; // returns false

a == x; // returns true

a != b; // returns true

a > b; // returns false

a < b; // returns true

a >= b; // returns false

a <= b; // returns true

Các toán tử let x = 5; x++; //post-increment, x will be 5 here and 6 in the next line ++x; //pre-increment, x will be 7 here x--; //post-decrement, x will be 7 here and 6 in the next line --x; //pre-decrement, x will be 5 here 1 và let x = 5; x++; //post-increment, x will be 5 here and 6 in the next line ++x; //pre-increment, x will be 7 here x--; //post-decrement, x will be 7 here and 6 in the next line --x; //pre-decrement, x will be 5 here 2 là các toán tử đơn. Nó chỉ hoạt động với toán hạng bên trái hoặc phải. Khi được sử dụng với toán hạng bên trái, ví dụ: let x = 5; x++; //post-increment, x will be 5 here and 6 in the next line ++x; //pre-increment, x will be 7 here x--; //post-decrement, x will be 7 here and 6 in the next line --x; //pre-decrement, x will be 5 here 3, nó sẽ tăng giá trị của let x = 5; x++; //post-increment, x will be 5 here and 6 in the next line ++x; //pre-increment, x will be 7 here x--; //post-decrement, x will be 7 here and 6 in the next line --x; //pre-decrement, x will be 5 here 4 khi kiểm soát chương trình chuyển sang câu lệnh tiếp theo. Theo cách tương tự, khi nó được sử dụng với toán hạng bên phải, ví dụ, let x = 5; x++; //post-increment, x will be 5 here and 6 in the next line ++x; //pre-increment, x will be 7 here x--; //post-decrement, x will be 7 here and 6 in the next line --x; //pre-decrement, x will be 5 here 5, nó sẽ tăng giá trị của let x = 5; x++; //post-increment, x will be 5 here and 6 in the next line ++x; //pre-increment, x will be 7 here x--; //post-decrement, x will be 7 here and 6 in the next line --x; //pre-decrement, x will be 5 here 4 chỉ có. Do đó, let x = 5; x++; //post-increment, x will be 5 here and 6 in the next line ++x; //pre-increment, x will be 7 here x--; //post-decrement, x will be 7 here and 6 in the next line --x; //pre-decrement, x will be 5 here 3 được gọi là sau khi tăng cường và let x = 5; x++; //post-increment, x will be 5 here and 6 in the next line ++x; //pre-increment, x will be 7 here x--; //post-decrement, x will be 7 here and 6 in the next line --x; //pre-decrement, x will be 5 here 5 được gọi là trước khi tăng.

Kết hợp chuỗi

Nhà điều hành Sự mô tả
+ Thêm hai toán hạng số.
- Trừ toán hạng bên phải từ toán hạng bên trái
* Nhân hai toán hạng số.

let a = 5, b = 10;

(a != b) && (a < b); // returns true

(a > b) || (a == b); // returns false

(a < b) || (a == b); // returns true

!(a < b); // returns false

!(a > b); // returns true

/

Chia hạng bên trái cho toán hạng bên phải.

Phần trăm Sự mô tả
+ Thêm hai toán hạng số.
- Trừ toán hạng bên phải từ toán hạng bên trái
* Nhân hai toán hạng số.
/ Chia hạng bên trái cho toán hạng bên phải.
Phần trăm Toán tử mô đun. Trả lại phần còn lại của hai toán hạng.
++ Toán tử gia tăng. Tăng giá trị toán hạng lên một.

let x = 5, y = 10, z = 15;

x = y; //x would be 10

x += 1; //x would be 6

x -= 1; //x would be 4

x *= 5; //x would be 25

x /= 5; //x would be 1

x %= 2; //x would be 1

Toán tử ternary

-

 ?  : ;

Toán tử giảm. Giảm giá trị của một.

let a = 10, b = 5;

let c = a > b? a : b; // value of c would be 10
let d = a > b? b : a; // value of d would be 5

  1. JavaScript bao gồm các toán tử thực hiện một số hoạt động trên một hoặc nhiều toán hạng (giá trị dữ liệu) và tạo ra kết quả.
  2. JavaScript bao gồm các loại toán tử khác nhau: toán tử số học, toán tử so sánh, toán tử logic, toán tử chuyển nhượng, toán tử có điều kiện.
  3. Toán tử ternary?: Là một dạng ngắn của điều kiện if-else.

Bạn muốn kiểm tra xem bạn biết JavaScript bao nhiêu?

4 loại toán tử JavaScript là gì?

JavaScript bao gồm các loại toán tử khác nhau: toán tử số học, toán tử so sánh, toán tử logic, toán tử chuyển nhượng, toán tử có điều kiện.Arithmetic operators, Comparison operators, Logical operators, Assignment operators, Conditional operators.

Các loại toán tử khác nhau trong JavaScript là gì?

Các toán tử so sánh JavaScript.

4 loại toán tử là gì?

Operators..
toán tử số học ..
người vận hành quan hệ ..
toán tử logic ..

7 toán tử JavaScript phổ biến nhất là gì?

7 toán tử phổ biến trong JavaScript..
Các nhà khai thác số học:.
Toán tử chuyển nhượng:.
Toán tử logic:.
Toán tử so sánh..
Các nhà khai thác bitwise ..
Toán tử chuỗi ..
Nhà điều hành Ternary ..