Trong hướng dẫn này, bạn sẽ tìm hiểu về các toán tử khác nhau có sẵn trong JavaScript và cách sử dụng chúng với sự trợ giúp của các ví dụ.
Nhà điều hành là gì?
Trong JavaScript, một toán tử là một ký hiệu đặc biệt được sử dụng để thực hiện các phép toán trên các toán hạng (giá trị và biến). Ví dụ,
2 + 3; // 5
Đây +
là một toán tử thực hiện phép cộng và 2
và 3
là các toán hạng.
Các loại toán tử JavaScript
Dưới đây là danh sách các toán tử khác nhau mà bạn sẽ học trong hướng dẫn này.
- Người điều hành nhiệm vụ
- Toán tử số học
- Toán tử so sánh
- Toán tử logic
- Toán tử Bitwise
- Toán tử chuỗi
- Các nhà khai thác khác
Toán tử gán JavaScript
Toán tử gán được sử dụng để gán giá trị cho các biến. Ví dụ,
const x = 5;
Ở đây, =
toán tử được sử dụng để gán giá trị 5
cho biến x
.
Dưới đây là danh sách các toán tử gán thường được sử dụng:
Nhà điều hành | Tên | Thí dụ |
---|---|---|
= | Toán tử chuyển nhượng | a = 7; // 7 |
+= | Bài tập bổ sung | a += 5; // a = a + 5 |
-= | Phép trừ | a -= 2; // a = a - 2 |
*= | Phép nhân | a *= 3; // a = a * 3 |
/= | Phân công bộ phận | a /= 2; // a = a / 2 |
%= | Chuyển nhượng phần còn lại | a %= 2; // a = a % 2 |
**= | Phép tính lũy thừa | a **= 2; // a = a**2 |
Lưu ý: Toán tử gán thường được sử dụng là =
. Bạn sẽ hiểu toán tử gán khác như +=
, -=
, *=
vv Một khi chúng ta học toán tử số học.
Toán tử số học JavaScript
Toán tử số học được sử dụng để thực hiện các phép tính số học . Ví dụ,
const number = 3 + 5; // 8
Ở đây, +
toán tử được sử dụng để thêm hai toán hạng.
Nhà điều hành | Tên | Thí dụ |
---|---|---|
+ | Thêm vào | x + y |
- | Phép trừ | x - y |
* | Phép nhân | x * y |
/ | Sư đoàn | x / y |
% | Phần còn lại | x % y |
++ | Tăng (tăng 1) | ++x hoặc là x++ |
-- | Giảm (giảm 1) | --x hoặc là x-- |
** | Luỹ thừa (Công suất) | x ** y |
Ví dụ 1: Các toán tử số học trong JavaScript
let x = 5; let y = 3; // addition console.log('x + y = ', x + y); // subtraction console.log('x - y = ', x - y); // multiplication console.log('x * y = ', x * y); // division console.log('x / y = ', x / y); // remainder console.log('x % y = ', x % y); // increment console.log('++x = ', ++x); // x is now 6 console.log('x++ = ', x++); // x returns 6 and then increases by 1 console.log('x = ', x); // decrement console.log('--x = ', --x); // x is now 6 console.log('x-- = ', x--); // x returns 6 and then increases by 1 console.log('x = ', x); //exponentiation console.log('x ** y =', x ** y);
Truy cập ++ và - toán tử để tìm hiểu thêm.
Đầu ra
x + y = 8 x - y = 2 x * y = 15 x / y = 1.6666666666666667 x% y = 2 ++ x = 6 x ++ = 6 x = 7 --x = 6 x-- = 6 x = 5 x ** y = 125
Lưu ý : Toán tử ** đã được giới thiệu trong EcmaScript 2016 và một số trình duyệt có thể không hỗ trợ chúng. Để tìm hiểu thêm, hãy truy cập hỗ trợ trình duyệt lũy thừa JavaScript.
Toán tử so sánh JavaScript
Toán tử so sánh so sánh hai giá trị và trả về giá trị boolean, hoặc true
hoặc false
. Ví dụ,
const a = 3, b = 2; console.log(a> b); // true
Ở đây, toán tử so sánh >
được sử dụng để so sánh xem a có lớn hơn b hay không.
Nhà điều hành | Sự miêu tả | Thí dụ |
---|---|---|
== | Equal to: returns true if the operands are equal | x == y |
!= | Not equal to: returns true if the operands are not equal | x != y |
=== | Strict equal to: true if the operands are equal and of the same type | x === y |
!== | Strict not equal to: true if the operands are equal but of different type or not equal at all | x !== y |
> | Greater than: true if left operand is greater than the right operand | x> y |
>= | Greater than or equal to: true if left operand is greater than or equal to the right operand | x>= y |
< | Less than: true if the left operand is less than the right operand | x < y |
<= | Less than or equal to: true if the left operand is less than or equal to the right operand | x <= y |
Example 2: Comparison operators in JavaScript
// equal operator console.log(2 == 2); // true console.log(2 == '2'); // true // not equal operator console.log(3 != 2); // true console.log('hello' != 'Hello'); // true // strict equal operator console.log(2 === 2); // true console.log(2 === '2'); // false // strict not equal operator console.log(2 !== '2'); // true console.log(2 !== '2'); // false
Output
true true true true true false false true
Comparison operators are used in decision making and loops. You will learn about the use of comparison operators in detail in later tutorials.
JavaScript Logical Operators
Logical operators perform logical operations and return a boolean value, either true
or false
. For example,
const x = 5, y = 3; (x < 6) && (y < 5); // true
Here, &&
is the logical operator AND. Since both x < 6
and y < 5
are true
, the result is true
.
Operator | Description | Example |
---|---|---|
&& | Logical AND: true if both the operands are true , else returns false | x && y |
|| | Logical OR: true if either of the operands is true ; returns false if both are false | x || y |
! | Logical NOT: true if the operand is false and vice-versa. | !x |
Example 3: Logical Operators in JavaScript
// logical AND console.log(true && true); // true console.log(true && false); // false // logical OR console.log(true || false); // true // logical NOT console.log(!true); // false
Output
true false true false
Logical operators are used in decision making and loops. You will learn about the use of logical operators in detail in later tutorials.
JavaScript Bitwise Operators
Bitwise operators perform operations on binary representations of numbers.
Operator | Description |
---|---|
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise XOR |
~ | Bitwise NOT |
<< | Left shift |
>> | Sign-propagating right shift |
>>> | Zero-fill right shift |
Bitwise operators are rarely used in everyday programming. If you are interested, visit JavaScript Bitwise Operators to learn more.
JavaScript String Operators
In JavaScript, you can also use the +
operator to concatenate (join) two or more strings.
Example 4: String operators in JavaScript
// concatenation operator console.log('hello' + 'world'); let a = 'JavaScript'; a += ' tutorial'; // a = a + ' tutorial'; console.log(a);
Output
helloworld JavaScript tutorial
Lưu ý: Khi +
được sử dụng với chuỗi, nó thực hiện nối. Tuy nhiên, khi +
được sử dụng với các số, nó sẽ thực hiện phép cộng.
Các toán tử JavaScript khác
Đây là danh sách các toán tử khác có sẵn trong JavaScript. Bạn sẽ tìm hiểu về các toán tử này trong các hướng dẫn sau.
Nhà điều hành | Sự miêu tả | Thí dụ |
---|---|---|
, | đánh giá nhiều toán hạng và trả về giá trị của toán hạng cuối cùng. | let a = (1, 3 , 4); // 4 |
?: | trả về giá trị dựa trên điều kiện | (5> 3) ? 'success' : 'error'; // "success" |
delete | xóa thuộc tính của một đối tượng hoặc một phần tử của mảng | delete x |
typeof | trả về một chuỗi chỉ ra kiểu dữ liệu | typeof 3; // "number" |
void | loại bỏ giá trị trả về của biểu thức | void(x) |
in | trả về true nếu thuộc tính được chỉ định nằm trong đối tượng | prop in object |
instanceof | trả về true nếu đối tượng được chỉ định thuộc loại đối tượng được chỉ định | object instanceof object_type |