JavaScript bao gồm các toán tử giống như các ngôn ngữ khác. Toán tử thực hiện một số thao tác 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 phép cộng hai giá trị số và trả về kết quả
JavaScript bao gồm các danh mục toán tử sau
toán tử số học
Các toán tử số học được sử dụng để thực hiện các phép toán giữa các toán hạng số
Toán tử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 toán hạng bên trái cho toán hạng bên phải. Toán tử %mô-đun. Trả về 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. --Decrement toán tử. Giảm giá trị đi một
Ví dụ sau minh họa cách toán tử số học thực hiện các tác vụ khác nhau trên toán hạng
Thí dụ. Phép toán số học
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
Thử nó
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à toán tử một ngôi. Nó chỉ hoạt động với toán hạng trái hoặc phải. Khi được sử dụng với toán hạng bên trái, e. g. , 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 điều khiển 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, e. g. , 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ẽ làm 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 ở đó thôi. 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à tăng sau 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à tăng trướcThí dụ. Tăng/giảm sau và trước
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
Thử nó
nối 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 minh họa nối chuỗi ngay cả khi một trong các toán hạng là một chuỗi
Thí dụ. + Toán Tử Với 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
Thử nó
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
1OperatorsDescription==Compares the equality of two operands without considering type.===Compares equality of two operands with type.!=Compares inequality of two operands.>Returns a boolean value true if the left-side value is greater than the right-side value; otherwise, returns false.=Returns a boolean value true if the left-side value is greater than or equal to the right-side value; otherwise, returns false. b; // returns false a < b; // returns true a >= b; // returns false a b] || [a == b]; // returns false [a < b] || [a == b]; // returns true ![a < b]; // returns false ![a > b]; // returns true
Thử nó
Toán tử gán
JavaScript cung cấp các toán tử gán để gán giá trị cho các biến với ít thao tác gõ phím hơn
Toán tử gánDescription=Gán giá trị toán hạng bên phải cho toán hạng bên trái. +=Tính tổng các giá trị toán hạng bên trái và bên phải và gán kết quả cho toán hạng bên trái. -=Trừ giá trị toán hạng bên phải từ giá trị toán hạng bên trái và gán kết quả cho toán hạng bên trái. *=Nhân các giá trị toán hạng trái và phải và gán kết quả cho toán hạng trái. /=Chia giá trị toán hạng bên trái cho giá trị toán hạng bên phải và gán kết quả cho toán hạng bên trái. %=Lấy mô đun của toán hạng bên trái chia cho toán hạng bên phải và gán mô đun kết quả cho toán hạng bên tráiThí dụ. Toán tử gán
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
Thử nó
Toán tử bậc ba
JavaScript cung cấp một toán tử đặc biệt có tên là toán tử bậc ba
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
6 gán giá trị cho một biến dựa trên một số điều kiện. Đây là dạng rút gọn của điều kiện if othercú pháp
? : ;
Toán tử bậc ba bắt đầu bằng biểu thức điều kiện theo sau là toán tử
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
7. Phần thứ hai [sau 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
7 và trướ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
9] sẽ được thực hiện nếu điều kiện trở thành đúng. Giả sử, điều kiện trả về 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, thì phần thứ ba [sau. ] sẽ được thực hiện