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

OperatorNameExample

const x = 5;
4Assignment operator
const x = 5;
8
const x = 5;
9Addition assignment
const number = 3 + 5; // 8
0
const number = 3 + 5; // 8
1Subtraction Assignment
const number = 3 + 5; // 8
2
const number = 3 + 5; // 8
3Multiplication Assignment
const number = 3 + 5; // 8
4
const number = 3 + 5; // 8
5Division Assignment
const number = 3 + 5; // 8
6
const number = 3 + 5; // 8
7Remainder Assignment
const number = 3 + 5; // 8
8
const number = 3 + 5; // 8
9Exponentiation Assignment
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);
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; // 8
1,
const number = 3 + 5; // 8
3, 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

OperatorNameExample

const x = 5;
1Addition
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);
7
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);
8Subtraction
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);
9
const a = 3, b = 2;
console.log(a > b); // true 
0Multiplication
const a = 3, b = 2;
console.log(a > b); // true 
1
const a = 3, b = 2;
console.log(a > b); // true 
2Division
const a = 3, b = 2;
console.log(a > b); // true 
3
const a = 3, b = 2;
console.log(a > b); // true 
4Remainder
const a = 3, b = 2;
console.log(a > b); // true 
5
const 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 
8
const 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;
14
const x = 5;
15returns value based on the condition
const x = 5;
16
const x = 5;
17deletes an object's property, or an element of an array
const x = 5;
18
const x = 5;
19returns a string indicating the data type
const x = 5;
20
const x = 5;
21discards the expression's return value
const x = 5;
22
const 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 object
const x = 5;
25
const 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 type
const 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