คณิตศาสตร์ JavaScript

สถานการณ์ที่มีทั้งสองตัวเลขคือการคำนวณ

สัญญาณคณิตศาสตร์ JavaScript

สัญญาณคณิตศาสตร์ทำการคณิตศาสตร์กับตัวเลข (ตัวอักษรหรือตัวแปร)

operator description
+ การเพิ่ม
- การลด
* การคูณ
** ระดับ (ES2016)
/ การหาร
% ตัวเลขเชิงจำนวน
++ เพิ่มค่าเชิงจำนวน
-- ลดค่าเชิงจำนวน

สัญญาณคณิตศาสตร์

การทำการคณิตศาสตร์ทั่วไปจะทำการกับสองตัวเลข

ทั้งสองตัวเลขนี้จะเป็น

instance

var x = 7 + 8;

try it yourself

หรือตัวแปร

instance

var x = a + b;

try it yourself

หรือแสดงกรณี

instance

var x = (7 + 8) * a;

try it yourself

สัญญาณและตัวเลขที่ทำการ

ในการคำนวณ ตัวเลขจะเรียกว่าตัวเลขที่ทำการ.

การทำการ (ที่ทำระหว่างสองตัวเลข) โดยoperatorการระบุ

ตัวเลขที่ทำการ operator ตัวเลขที่ทำการ
7 + 8

การเพิ่ม

การเพิ่มสัญญาณ (+ฉันแล้วกลับตัวเลขที่เป็นตัวเลขที่เพิ่มค่า

var x = 7;
var y = 8;
var z = x + y; 

try it yourself

การลด

การลดสัญญาณ (-ฉันแล้วกลับตัวเลขที่เป็นตัวเลขที่ลดค่า

var x = 7;
var y = 8;
var z = x - y; 

try it yourself

การคูณ

การคูณสัญญาณ (*ฉันแล้วกลับตัวเลขที่เป็นตัวเลขที่มีหน่วยเชิงจำนวน

var x = 7;
var y = 8;
var z = x * y; 

try it yourself

การหาร

การหารสัญญาณ (/ฉันแล้วกลับตัวเลขที่เป็นหน่วยเชิงจำนวน

var x = 7;
var y = 2;
var z = x / y; 

try it yourself

ตัวเลขเชิงจำนวน

ตัวเลขเชิงจำนวนสัญญาณ (%ฉันแล้วกลับค่าหน่วยเชิงจำนวนที่เหลือเมื่อหาร

var x = 7;
var y = 2;
var z = x % y; 

try it yourself

หมายเหตุ:ในการคำนวณ การหารสองตัวเลขจะมีผลลัพธ์คือค่าเชิงจำนวนและหน่วยเชิงจำนวนที่เหลือ

หมายเหตุ:ในคณิตศาสตร์ ผลลัพธ์ของการหารเชิงจำนวนคือหน่วยเชิงจำนวนที่เหลือเมื่อหาร

เพิ่มค่าเชิงจำนวน

เพิ่มค่าเชิงจำนวนสัญญาณ (++ฉันแล้วเพิ่มค่าเลขเชิงจำนวน

var x = 7;
x++;
var z = x;

try it yourself

ลดค่าเชิงจำนวน

ลดค่าเชิงจำนวนสัญญาณ (--ฉันแล้วลดค่าเลขเชิงจำนวน

var x = 7;
x--;
var z = x; 

try it yourself

ระดับ

การทำระดับ (**) จะเพิ่มค่าแรกเป็นอำนาจของค่าที่สอง

instance

var x = 5;
var z = x ** 2;          // ผลลัพธ์คือ 25

try it yourself

The result of x ** y is the same as Math.pow(x,y):

instance

var x = 5;
var z = Math.pow(x,2);   // The result is 25

try it yourself

Operator precedence

Operator precedence (Operator precedence) describes the order of operations performed in arithmetic expressions.

instance

var x = 200 + 50 * 2;

try it yourself

In the example above, is the result 250 * 2 or 200 + 100?

Is addition or multiplication given priority?

In traditional school mathematics, multiplication is given priority.

multiplication(*)and division(%)and addition(+)and subtraction(-)has a higherprecedence.

At the same time, (just like in school mathematics) you can change the precedence by using parentheses:

instance

var x = (200 + 50) * 2;

try it yourself

When using parentheses, the operators within the parentheses are calculated first.

When multiple operators have the same precedence (such as addition and subtraction), their calculations are from left to right:

instance

var x = 200 + 50 - 2;

try it yourself

JavaScript operator precedence value

value operator description instance
20 ( ) expression grouping (3 + 4)
       
19 . member person.name
19 [] member person["name"]
19 () function call myFunction()
19 new create new Date()
       
17 ++ postfix increment i++
17 -- postfix decrement i--
       
16 ++ prefix increment ++i
16 -- prefix decrement --i
16 ! ปฏิเสธเชิงโลก !(x==y)
16 typeof type typeof x
       
15 ** power (ES7) 10 ** 2
       
14 * multiply 10 * 5
14 / divide 10 / 5
14 % modulus division 10 % 5
       
13 + add 10 + 5
13 - subtract 10 - 5
       
12 << left shift x << 2
12 >> right shift x >> 2
12 >>> right shift (unsigned) x >>> 2
       
11 < less than x < y
11 <= less than or equal x <= y
11 > greater than x > y
11 >= greater than or equal x >= y
11 in property in object "PI" in Math
11 instanceof instance of object instanceof Array
       
10 == equal x == y
10 === strictly equal x === y
10 != ไม่เท่ากัน x != y
10 !== ไม่เท่ากันอย่างเฉพาะ x !== y
       
9 & & ทางตำแหน่ง x & y
8 ^ XOR ทางตำแหน่ง x ^ y
7 | หรือทางตำแหน่ง x | y
6 && เชิงโลก x && y
5 || ปฏิเสธเชิงโลก x || y
4 ? : เงื่อนไข ? "Yes" : "No"
       
3 = กำหนดค่า x = y
3 += กำหนดค่า x += y
3 -= กำหนดค่า x -= y
3 *= กำหนดค่า x *= y
3 %= กำหนดค่า x %= y
3 <<= กำหนดค่า x <<= y
3 >>= กำหนดค่า x >>= y
3 >>>= กำหนดค่า x >>>= y
3 &= กำหนดค่า x &= y
3 ^= กำหนดค่า x ^= y
3 |= กำหนดค่า x |= y
       
2 yield หยุดหน่วยงาน yield x
1 , เช่น 7 , 8

จำเป็น:สีแดงอ่อนบ่งชี้เทคโนโลยีที่ทดสอบหรือแนะนำ (ECMASScript 2016 หรือ ES7)

คำแนะนำ:สัญญาณที่อยู่ในวงเล็บจะถูกคำนวณทั้งหมดก่อนที่จะถูกใช้ในส่วนที่เหลือของสัญญาณ