Trong JavaScript, toán tử là một ký hiệu đặc biệt được sử dụng để thực hiện các thao tác trên toán hạng [giá trị và biến]. Ví dụ,
2 + 3; // 5
Ở đây
const x = 5;
1 là toán tử thực hiện phép cộng, và const x = 5;
2 và const x = 5;
3 là toán hạngCác loại toán tử JavaScript
Đâ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
- Toán tử gán
- 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
- Toán tử khác
Toán tử gán JavaScript
Toán tử gán dùng để gán giá trị cho biến. Ví dụ,
const x = 5;
Ở đây, toán tử
const x = 5;
4 được sử dụng để gán giá trị const x = 5;
5 cho biến const x = 5;
6Dưới đây là danh sách các toán tử gán thường được sử dụng
OperatorNameExample
const x = 5;
4Assignment operatorconst x = 5;
8const x = 5;
9Addition assignmentconst number = 3 + 5; // 8
0const number = 3 + 5; // 8
1Subtraction Assignmentconst number = 3 + 5; // 8
2const number = 3 + 5; // 8
3Multiplication Assignmentconst number = 3 + 5; // 8
4const number = 3 + 5; // 8
5Division Assignmentconst number = 3 + 5; // 8
6const number = 3 + 5; // 8
7Remainder Assignmentconst number = 3 + 5; // 8
8const number = 3 + 5; // 8
9Exponentiation Assignmentlet x = 5;
let y = 3;
// addition
console.log['x + y = ', x + y]; // 8
// subtraction
console.log['x - y = ', x - y]; // 2
// multiplication
console.log['x * y = ', x * y]; // 15
// division
console.log['x / y = ', x / y]; // 1.6666666666666667
// remainder
console.log['x % y = ', x % y]; // 2
// increment
console.log['++x = ', ++x]; // x is now 6
console.log['x++ = ', x++]; // prints 6 and then increased to 7
console.log['x = ', x]; // 7
// decrement
console.log['--x = ', --x]; // x is now 6
console.log['x-- = ', x--]; // prints 6 and then decreased to 5
console.log['x = ', x]; // 5
//exponentiation
console.log['x ** y =', x ** y];
0Ghi chú. Toán tử gán thường được sử dụng là
const x = 5;
4. Bạn sẽ hiểu các toán tử gán khác như const x = 5;
9, const number = 3 + 5; // 8
1, const number = 3 + 5; // 8
3, v.v. một khi chúng ta học toán tử số họcToán tử số học JavaScript
Các 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ử
const x = 5;
1 được sử dụng để cộng hai toán hạngOperatorNameExample
const x = 5;
1Additionlet x = 5;
let y = 3;
// addition
console.log['x + y = ', x + y]; // 8
// subtraction
console.log['x - y = ', x - y]; // 2
// multiplication
console.log['x * y = ', x * y]; // 15
// division
console.log['x / y = ', x / y]; // 1.6666666666666667
// remainder
console.log['x % y = ', x % y]; // 2
// increment
console.log['++x = ', ++x]; // x is now 6
console.log['x++ = ', x++]; // prints 6 and then increased to 7
console.log['x = ', x]; // 7
// decrement
console.log['--x = ', --x]; // x is now 6
console.log['x-- = ', x--]; // prints 6 and then decreased to 5
console.log['x = ', x]; // 5
//exponentiation
console.log['x ** y =', x ** y];
7let x = 5;
let y = 3;
// addition
console.log['x + y = ', x + y]; // 8
// subtraction
console.log['x - y = ', x - y]; // 2
// multiplication
console.log['x * y = ', x * y]; // 15
// division
console.log['x / y = ', x / y]; // 1.6666666666666667
// remainder
console.log['x % y = ', x % y]; // 2
// increment
console.log['++x = ', ++x]; // x is now 6
console.log['x++ = ', x++]; // prints 6 and then increased to 7
console.log['x = ', x]; // 7
// decrement
console.log['--x = ', --x]; // x is now 6
console.log['x-- = ', x--]; // prints 6 and then decreased to 5
console.log['x = ', x]; // 5
//exponentiation
console.log['x ** y =', x ** y];
8Subtractionlet x = 5;
let y = 3;
// addition
console.log['x + y = ', x + y]; // 8
// subtraction
console.log['x - y = ', x - y]; // 2
// multiplication
console.log['x * y = ', x * y]; // 15
// division
console.log['x / y = ', x / y]; // 1.6666666666666667
// remainder
console.log['x % y = ', x % y]; // 2
// increment
console.log['++x = ', ++x]; // x is now 6
console.log['x++ = ', x++]; // prints 6 and then increased to 7
console.log['x = ', x]; // 7
// decrement
console.log['--x = ', --x]; // x is now 6
console.log['x-- = ', x--]; // prints 6 and then decreased to 5
console.log['x = ', x]; // 5
//exponentiation
console.log['x ** y =', x ** y];
9const a = 3, b = 2;
console.log[a > b]; // true
0Multiplicationconst a = 3, b = 2;
console.log[a > b]; // true
1const a = 3, b = 2;
console.log[a > b]; // true
2Divisionconst a = 3, b = 2;
console.log[a > b]; // true
3const a = 3, b = 2;
console.log[a > b]; // true
4Remainderconst a = 3, b = 2;
console.log[a > b]; // true
5const a = 3, b = 2;
console.log[a > b]; // true
6Increment [increments by 1]const a = 3, b = 2;
console.log[a > b]; // true
7 or const a = 3, b = 2;
console.log[a > b]; // true
8const a = 3, b = 2;
console.log[a > b]; // true
9Decrement [decrements by 1]// 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
0 or // 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
1// 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
2Exponentiation [Power]// 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
3ví dụ 1. Toán tử số học trong JavaScript
let x = 5;
let y = 3;
// addition
console.log['x + y = ', x + y]; // 8
// subtraction
console.log['x - y = ', x - y]; // 2
// multiplication
console.log['x * y = ', x * y]; // 15
// division
console.log['x / y = ', x / y]; // 1.6666666666666667
// remainder
console.log['x % y = ', x % y]; // 2
// increment
console.log['++x = ', ++x]; // x is now 6
console.log['x++ = ', x++]; // prints 6 and then increased to 7
console.log['x = ', x]; // 7
// decrement
console.log['--x = ', --x]; // x is now 6
console.log['x-- = ', x--]; // prints 6 and then decreased to 5
console.log['x = ', x]; // 5
//exponentiation
console.log['x ** y =', x ** y];
Truy cập toán tử ++ và -- để tìm hiểu thêm
Ghi chú. Toán tử
// 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
2 đã đượ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ậpToán tử so sánh JavaScript
Toán tử so sánh so sánh hai giá trị và trả về một giá trị boolean, hoặc là
// 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
5 hoặc là // 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
6. Ví dụ,const a = 3, b = 2;
console.log[a > b]; // true
Ở đây, toán tử so sánh
// 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
7 được sử dụng để so sánh xem a có lớn hơn b hay khôngToán tử Mô tảVí dụ
const x = 5;
13đánh giá nhiều toán hạng và trả về giá trị của toán hạng cuối cùng. const x = 5;
14const x = 5;
15returns value based on the conditionconst x = 5;
16const x = 5;
17deletes an object's property, or an element of an arrayconst x = 5;
18const x = 5;
19returns a string indicating the data typeconst x = 5;
20const x = 5;
21discards the expression's return valueconst x = 5;
22const x = 5;
23returns // 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
5 if the specified property is in the objectconst x = 5;
25const x = 5;
26returns // 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
5 if the specified object is of of the specified object typeconst x = 5;
28
Là gì ?. toán tử trong JavaScript?
Toán tử điều kiện [ternary] là toán tử JavaScript duy nhất có ba toán hạng. một điều kiện theo sau bởi một dấu chấm hỏi [ ? ], sau đó là một biểu thức để thực hiện nếu điều kiện là đúng theo sau bởi một dấu hai chấm [. ], và cuối cùng là biểu thức để thực hiện nếu điều kiện sai
Chúng ta có thể sử dụng in trong JavaScript không?
Toán tử in trả về true nếu thuộc tính đã chỉ định nằm trong đối tượng đã chỉ định hoặc chuỗi nguyên mẫu của nó .
Toán tử in hoạt động như thế nào?
Trong Python, toán tử in xác định xem một giá trị đã cho có phải là thành phần cấu thành của một chuỗi như chuỗi, mảng, danh sách hay bộ hay không. When used in a condition, the statement returns a Boolean result of True or False. The statement returns True if the specified value is found within the sequence.
Là gì '. ' Trong JavaScript?
Bất đẳng thức [. = ] kiểm tra xem hai toán hạng của nó có bằng nhau hay không, trả về kết quả Boolean . Không giống như toán tử bất đẳng thức nghiêm ngặt, nó cố gắng chuyển đổi và so sánh các toán hạng thuộc các kiểu khác nhau.
Dấu 3 chấm trong JS là gì?
[ba dấu chấm trong JavaScript] được gọi là Cú pháp trải hoặc Toán tử trải . Điều này cho phép một iterable chẳng hạn như một biểu thức mảng hoặc chuỗi được mở rộng hoặc một biểu thức đối tượng được mở rộng bất cứ nơi nào được đặt.
Bạn có thể sử dụng toán tử in trên mảng JavaScript không?
Toán tử "in" cũng có thể được sử dụng trên Mảng , vì Mảng về mặt kỹ thuật là Đối tượng trong JavaScript. Sự khác biệt ở đây là toán tử "in" chỉ có thể được sử dụng để tìm hiểu xem một chỉ mục nhất định có nằm trong Mảng hay không, vì các giá trị trong một mảng không phải là thuộc tính của mảng.