javascript运算符_JavaScript模,除法,余数和其他数学运算符的解释

javascript运算符

JavaScript provides the user with five arithmetic operators: +, -, *, / and %. The operators are for addition, subtraction, multiplication, division and remainder (or modulo), respectively.

JavaScript为用户提供了五个算术运算符: +-*/% 。 运算符分别用于加法,减法,乘法,除法和余数(或模)。

加成 (Addition)

Syntax

句法

a + b

a + b

Usage

用法

2 + 3          // returns 5
true + 2       // interprets true as 1 and returns 3
false + 5      // interprets false as 0 and returns 5
true + "bar"   // concatenates the boolean value and returns "truebar"
5 + "foo"      // concatenates the string and the number and returns "5foo"
"foo" + "bar"  // concatenates the strings and returns "foobar"

Hint: There is a handy increment) operator that is a great shortcut when you’re adding numbers by 1.

提示:有一个方便的递增 )运算符,当您将数字加1时,这是一个很好的快捷方式。

减法 (Subtraction)

Syntax

句法

a - b

a - b

Usage

用法

2 - 3      // returns -1
3 - 2      // returns 1
false - 5  // interprets false as 0 and returns -5
true + 3   // interprets true as 1 and returns 4
5 + "foo"  // returns NaN (Not a Number)

Hint: There is a handy decrement) operator that is a great shortcut when you’re subtracting numbers by 1.

提示:有一个方便的递减 )运算符,当您将数字减1时,这是一个很好的捷径。

乘法 (Multiplication)

Syntax

句法

a * b

a * b

Usage

用法

2 * 3                // returns 6
3 * -2               // returns -6
false * 5            // interprets false as 0 and returns 0
true * 3             // interprets true as 1 and returns 3
5 * "foo"            // returns NaN (Not a Number)
Infinity * 0         // returns NaN
Infinity * Infinity  // returns Infinity

(Division)

Syntax

句法

a / b

a / b

Usage

用法

3 / 2                // returns 1.5
3.0 / 2/0            // returns 1.5
3 / 0                // returns Infinity
3.0 / 0.0            // returns Infinity
-3 / 0               // returns -Infinity
false / 5            // interprets false as 0 and returns 0
true / 2             // interprets true a 1 and returns 0.5
5 + "foo"            // returns NaN (Not a Number)
Infinity / Infinity  // returns NaN

(Remainder)

Syntax

句法

a % b

a % b

Usage

用法

3 % 2          // returns 1
true % 5       // interprets true as 1 and returns 1
false % 4      // interprets false as 0 and returns 0
3 % "bar"      // returns NaN

增量 (Increment)

Syntax

句法

a++ or ++a

a++ or ++a

Usage// Postfix x = 3; // declare a variable y = x++; // y = 4, x = 3// Prefix var a = 2; b = ++a; // a = 3, b = 3

用法 //后缀x = 3; //声明变量y = x ++; // y = 4,x = 3 //前缀var a = 2; b = ++ a; // a = 3,b = 3

减量 (Decrement)

Syntax

句法

a-- or --a

a-- or --a

Usage// Postfix x = 3; // declare a variable y = x—; // y = 3, x = 3// Prefix var a = 2; b = —a; // a = 1, b = 1 !Important! As you can see, you cannot perform any sort of operations on Infinity.

用法 //后缀x = 3; //声明变量y = x—; // y = 3,x = 3 //前缀var a = 2; b = -a; // a = 1,b = 1 重要! 如您所见,您无法Infinity上执行任何类型的操作。

有关JavaScript中数学的更多信息: (More on math in JavaScript:)

翻译自: https://www.freecodecamp.org/news/javascript-arithmetic-math-operators-explained/

javascript运算符

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值