Các phép toán số học trong JavaScript

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ạng

Cá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;6

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

OperatorNameExampleconst x = 5;4Assignment operatorconst x = 5;8const x = 5;9Addition assignmentconst number = 3 + 5; // 80const number = 3 + 5; // 81Subtraction Assignmentconst number = 3 + 5; // 82const number = 3 + 5; // 83Multiplication Assignmentconst number = 3 + 5; // 84const number = 3 + 5; // 85Division Assignmentconst number = 3 + 5; // 86const number = 3 + 5; // 87Remainder Assignmentconst number = 3 + 5; // 88const number = 3 + 5; // 89Exponentiation 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);0

Ghi 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; // 81, const number = 3 + 5; // 83, v.v. một khi chúng ta học toán tử số học

Toá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ạng

OperatorNameExampleconst 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 3

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

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ập

Toá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ông

Toá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

Toán tử số học trong JavaScript là gì?

Toán tử số học lấy các giá trị số (cả chữ hoặc biến) làm toán hạng và trả về một giá trị số duy nhất . Toán tử lũy thừa. toán tử nhân. nhà điều hành bộ phận.

Làm cách nào để thực hiện các phép toán số học bằng JavaScript?

Thêm. Toán tử cộng (+) cộng các số. .
trừ. Toán tử trừ ( - ) trừ các số. .
nhân. Toán tử nhân (*) nhân các số. .
phân chia. Toán tử chia ( / ) chia các số. .
gia tăng. Toán tử tăng ( ++ ) tăng số. .
giảm dần

5 phép toán số học là gì?

Các toán tử số học thực hiện các phép toán cộng, trừ, nhân, chia, lũy thừa và mô đun .

7 toán tử số học là gì?

Có 7 toán tử số học trong Python. .
Phép cộng
phép trừ
Phép nhân
Phân công
mô đun
lũy thừa
Phân chia tầng

Chủ đề