Toán tử JavaScript

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, 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 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à 2và 3là 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.

  • Assignment Operators
  • Arithmetic Operators
  • Comparison Operators
  • Logical Operators
  • Bitwise Operators
  • String Operators
  • Other Operators

Toán tử gán JavaScript

Các 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ị 5cho biến x.

Dưới đây là danh sách các toán tử gán thường được sử dụng:

OperatorNameExample
=Assignment operatora = 7; // 7
+=Addition assignmenta += 5; // a = a + 5
-=Subtraction Assignmenta -= 2; // a = a - 2
*=Multiplication Assignmenta *= 3; // a = a * 3
/=Division Assignmenta /= 2; // a = a / 2
%=Remainder Assignmenta %= 2; // a = a % 2
**=Exponentiation Assignmenta **= 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.

OperatorNameExample
+Additionx + y
-Subtractionx - y
*Multiplicationx * y
/Divisionx / y
%Remainderx % y
++Increment (increments by 1)++x or x++
--Decrement (decrements by 1)--x or x--
**Exponentiation (Power)x ** y

Ví 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.

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 truehoặ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 .

OperatorDecfscriptionExample
==Equal to: returns true if the operands are equalx == y
!=Not equal to: returns true if the operands are not equalx != y
===Strict equal totrue if the operands are equal and of the same typex === y
!==Strict not equal totrue if the operands are equal but of different type or not equal at allx !== y
>Greater thantrue if left operand is greater than the right operandx > y
>=Greater than or equal totrue if left operand is greater than or equal to the right operandx >= y
<Less thantrue if the left operand is less than the right operandx < y
<=Less than or equal totrue if the left operand is less than or equal to the right operandx <= y

Ví dụ 2: Các toán tử so sánh trong 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

Các toán tử so sánh được sử dụng trong quá trình ra quyết định và các vòng lặp. Bạn sẽ tìm hiểu chi tiết về việc sử dụng các toán tử so sánh trong các hướng dẫn sau.

Toán tử logic JavaScript

Toán tử logic thực hiện các phép toán logic và trả về một giá trị boolean, hoặc truehoặc false. Ví dụ,

const x = 5, y = 3;
(x < 6) && (y < 5); // true

Đây &&là toán tử logic AND . Vì cả hai x < 6và y < 5hiện tại true, kết quả là true.

OperatorDecfscriptionExample
&&Logical ANDtrue if both the operands are true, else returns falsex && y
||Logical ORtrue if either of the operands is true; returns false if both are falsex || y
!Logical NOTtrue if the operand is false and vice-versa.!x

Ví dụ 3: Các toán tử logic trong 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

Đầu ra

true
false
true
false

Các toán tử logic được sử dụng trong việc ra quyết định và các vòng lặp. Bạn sẽ tìm hiểu chi tiết về việc sử dụng các toán tử logic trong các hướng dẫn sau.

Toán tử Bitwise JavaScript

Toán tử bitwise thực hiện các phép toán trên biểu diễn nhị phân của số.

OperatorDecfscription
&Bitwise AND
|Bitwise OR
^Bitwise XOR
~Bitwise NOT
<<Left shift
>>Sign-propagating right shift
>>>Zero-fill right shift

Toán tử bitwise hiếm khi được sử dụng trong lập trình hàng ngày. Nếu bạn quan tâm, hãy truy cập JavaScript Bitwise Operators để tìm hiểu thêm.

Toán tử chuỗi JavaScript

Trong JavaScript, bạn cũng có thể sử dụng +toán tử để nối (nối) hai hoặc nhiều chuỗi.

Ví dụ 4: Toán tử chuỗi trong JavaScript

// concatenation operator
console.log('hello' + 'world');

let a = 'JavaScript';

a += ' tutorial';  // a = a + ' tutorial';
console.log(a);

Đầu ra

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.

OperatorDecfscriptionExample
,evaluates multiple operands and returns the value of the last operand.let a = (1, 3 , 4); // 4
?:returns value based on the condition(5 > 3) ? 'success' : 'error'; // "success"
deletedeletes an object’s property, or an element of an arraydelete x
typeofreturns a string indicating the data typetypeof 3; // "number"
voiddiscards the expression’s return valuevoid(x)
inreturns true if the specified property is in the objectprop in object
instanceofreturns true if the specified object is of of the specified object typeobject instanceof object_type








Gõ tìm kiếm nhanh...