Các toán tử số học JavaScript
Các toán tử số học thực hiện số học trên các số [nghĩa đen hoặc biến].
+ | Phép cộng |
- | Phép trừ |
* | Phép nhân |
** | Số mũ [ES2016] |
/ | Phân công |
Phần trăm | Mô đun [phần còn lại] |
++ | Tăng |
- | Giảm dần |
Các phép tính toán học
Một hoạt động số học điển hình hoạt động trên hai số.
Hai con số có thể là chữ:
hoặc các biến:
hoặc biểu thức:
Toán tử và toán hạng
Các số [trong một hoạt động số học] được gọi là toán hạng.operands.
Hoạt động [được thực hiện giữa hai toán hạng] được xác định bởi một toán tử.operator.
100 | + | 50 |
Phép cộng
-addition operator [
a - b0] adds numbers:
Phép trừ
*subtraction operator [
a - b1] subtracts numbers.
Phép nhân
**multiplication operator [
a - b2] multiplies numbers.
Số mũ [ES2016]
/division operator [
a - b3] divides numbers.
Phân công
Phần trămmodulus operator [
a - b4] returns the division remainder.
Mô đun [phần còn lại]quotient and a remainder.
++modulo operation is the remainder of an arithmetic division.
Tăng
-increment operator [
a - b5] increments numbers.
Giảm dần
Các phép tính toán họcdecrement operator [
a - b6] decrements numbers.
Một hoạt động số học điển hình hoạt động trên hai số.
Hai con số có thể là chữ:exponentiation operator [
a - b7] raises the first operand to the power of the second operand.
hoặc các biến:
hoặc biểu thức:
Toán tử và toán hạng
Các số [trong một hoạt động số học] được gọi là toán hạng.
Hoạt động [được thực hiện giữa hai toán hạng] được xác định bởi một toán tử.
Thoại
Thêmprecedence than addition [
a - b0] and subtraction [
a - b1].
Toán tử bổ sung [
a - b0] thêm số:
Trừ đi
Toán tử trừ [
a - b1] trừ các số.
Nhân
Các toán tử số học JavaScript là các toán tử hoạt động dựa trên các giá trị số và trả về một giá trị số. Có nhiều nhà khai thác trong JavaScript. Mỗi toán tử được mô tả dưới đây cùng với ví dụ của nó. are the operators that operate upon the numerical values and return a numerical value. There are many operators in JavaScript. Each operator is described below along with its example.
1. Bổ sung [+] toán tử bổ sung có hai toán hạng số và cho tổng số của chúng. Nó cũng kết hợp hai chuỗi hoặc số. The addition operator takes two numerical operands and gives their numerical sum. It also concatenates two strings or numbers.
Syntax:
a + b
Example:
// Number + Number => Addition 1 + 2 gives 3 // Number + String => Concatenation 5 + "hello" gives "5Hello"
2. trừ [-] toán tử trừ đưa ra sự khác biệt của hai toán hạng dưới dạng giá trị số. The subtraction operator gives the difference of two operands in the form of numerical value.
Syntax:
a - b
Example:
// Number - Number => Subtraction 10 - 7 gives 3 "Hello" - 1 gives Nan
3. Nhân hóa [*] Toán tử nhân cung cấp sản phẩm của các toán hạng trong đó một toán hạng là nhân và phần khác là nhân. The multiplication operator gives the product of operands where one operand is multiplicand and another is multiplier.
Syntax:
a * b
Example:
// Number * Number => Multiplication 3 * 3 gives 9 -4 * 4 gives -16 Infinity * 0 gives NaN Infinity * Infinity gives Infinity 'hi' * 2 gives NaN
4 The division operator provides the quotient of its operands where the right operand is the divisor and the left operand is the dividend.
Syntax:
a / b
Example:
// Number / Number => Division 5 / 2 gives 2.5 1.0 / 2.0 gives 0.5 3.0 / 0 gives Infinity 4.0 / 0.0 gives Infinity, because 0.0 == 0 2.0 / -0.0 gives -Infinity
5. Mô đun [%] toán tử mô đun trả về phần còn lại còn lại khi cổ tức được chia cho một ước số. Toán tử mô đun còn được gọi là toán tử còn lại. Nó có dấu hiệu của cổ tức. The modulus operator returns the remainder left over when a dividend is divided by a divisor. The modulus operator is also known as remainder operator. It takes the sign of the dividend.
Syntax:
a % b
Example:
// Number % Number => Modulus of the number 9 % 5 gives 4 -12 % 5 gives -2 1 % -2 gives 1 5.5 % 2 gives 1.5 -4 % 2 gives -0 NaN % 2 gives NaN
6 Toán tử số mũ là liên kết đúng. & Nbsp; The exponentiation operator gives the result of raising the first operand to the power of the second operand. The exponentiation operator is right-associative.
Syntax:
// Number + Number => Addition 1 + 2 gives 3 // Number + String => Concatenation 5 + "hello" gives "5Hello"0
Trong JavaScript, không thể viết một biểu thức số mũ mơ hồ, tức là bạn không thể đặt một toán tử unary [+ / - / ~ /! / Delete / void] ngay trước số cơ sở.
Example:
// Number + Number => Addition 1 + 2 gives 3 // Number + String => Concatenation 5 + "hello" gives "5Hello"1
7. Tăng [++] Các toán tử gia tăng tăng [thêm một vào] toán hạng của nó và trả về một giá trị. The increment operator increments [adds one to] its operand and returns a value.
- Nếu được sử dụng Postfix & nbsp; với toán tử sau toán hạng [ví dụ: x ++], thì nó sẽ tăng và trả về giá trị trước khi tăng.
- Nếu được sử dụng tiền tố với toán tử trước khi toán hạng [ví dụ: ++ x], thì nó sẽ tăng và trả về giá trị sau khi tăng.
Syntax:
// Number + Number => Addition 1 + 2 gives 3 // Number + String => Concatenation 5 + "hello" gives "5Hello"2
Example:
// Number + Number => Addition 1 + 2 gives 3 // Number + String => Concatenation 5 + "hello" gives "5Hello"3
8. Giảm [ -] Các toán tử giảm giảm [trừ một từ] toán hạng của nó và trả về một giá trị. The decrement operator decrements [subtracts one from] its operand and returns a value.
- Nếu được sử dụng postfix, với toán tử sau toán hạng [ví dụ: x,], thì nó sẽ giảm và trả về giá trị trước khi giảm.
- Nếu được sử dụng tiền tố, với toán tử trước khi toán hạng [ví dụ:,], thì nó sẽ giảm và trả về giá trị sau khi giảm.
Syntax:
// Number + Number => Addition 1 + 2 gives 3 // Number + String => Concatenation 5 + "hello" gives "5Hello"4
Example:
// Number + Number => Addition 1 + 2 gives 3 // Number + String => Concatenation 5 + "hello" gives "5Hello"5
9. Unary [-] Đây là một toán tử đơn, tức là nó hoạt động trên một toán hạng duy nhất. Nó đưa ra sự phủ định của một toán hạng. This is a unary operator i.e. it operates on a single operand. It gives the negation of an operand.
Syntax:
// Number + Number => Addition 1 + 2 gives 3 // Number + String => Concatenation 5 + "hello" gives "5Hello"6
Example:
// Number + Number => Addition 1 + 2 gives 3 // Number + String => Concatenation 5 + "hello" gives "5Hello"7
10. Unary [+] Đây là một cách để chuyển đổi số không thành số thành một số. Mặc dù không phủ định [-] cũng có thể chuyển đổi những người không phải là số người, Unary Plus là cách nhanh nhất và ưa thích để chuyển đổi một cái gì đó thành một số, bởi vì nó không thực hiện bất kỳ hoạt động nào khác trên số. This is a way to convert a non-number into a number. Although unary negation [-] also can convert non-numbers, unary plus is the fastest and preferred way of converting something into a number, because it does not perform any other operations on the number.
Syntax:
// Number + Number => Addition 1 + 2 gives 3 // Number + String => Concatenation 5 + "hello" gives "5Hello"8
Example:
// Number + Number => Addition 1 + 2 gives 3 // Number + String => Concatenation 5 + "hello" gives "5Hello"9