Operators

JavaScript Operators (JavaScript运算符)

JavaScript Operators

JavaScript Operators (JavaScript运算符)

We know many operators from school: addition +, multiplication *, subtraction -, and so on. In this chapter, we will talk about the aspects of operators that are not covered by school arithmetic.

Terms: “unary”, “binary”, “operand”

Terms: “unary”, “binary”, “operand”

There is some terminology, which you should know before going on. (在继续之前,您应该了解一些术语。)

let x = 2;
x = -x;
console.log(x); // -2,  here was applied unary negation
let x = 2,  y = 3;
console.log(y - x); // 1, binary minus subtracts values

There are two different operators in the examples above, which share the same symbol: the negation operator, a unary operator, and the subtraction operator.

String concatenation, binary +

String concatenation, binary + (字符串串联,二进制+)

These special features of JavaScript operators are beyond school arithmetics. (JavaScript运算符的这些特殊功能超出了学校的算术范围。)

Generally the plus operator + sums numbers, but if the binary + is applied to strings, merging them:

let str = "my" + "string";
console.log(str); // mystring

If one of the operands is a string, the other one is transformed to a string as well:

console.log( '2' + 3 ); // "23"
console.log( 3 + '2' ); // "32"

Both the first operand or the second can be strings. The rule is simple anyway: if one of the operands is a string, the other one is converted into a string too.

Please, note that operations run from left to right and if there are two numbers which followed by a string, they will be added before being converted to a string:

console.log(3 + 2 + '1' ); // "51" and not "321"

String concatenation is one of the special features of the binary plus + . Other arithmetic operators work only with numbers converting their operands to numbers (subtraction, division). (字符串串联是二进制加号+的特殊功能之一。其他算术运算符仅适用于将操作数转换为数字的数字(减法、除法)。)

console.log( 2 - '1' ); // 1
console.log( '6' / '2' ); // 3

Numeric conversion, unary +

Numeric conversion, unary + (数字转换,一元+)

The plus + operator exists in two forms: the binary form and the unary form.

The plus operator + applied to a single value and it does not do anything to numbers. But when the operand is not a number, the unary + will convert it into a number:

// this doesn’t effect numbers 
let x = 1;
console.log( +x ); // 1
let y = -2;
console.log( +y ); // -2

// converts not numbers
console.log( +true ); // 1
console.log( +"" );   // 0

Assignment =

Assignment = (作业)

We use the assignment operator (=) to assign a value to the JavaScript variable, it always returns a value. It is obvious for most of them like addition + or multiplication . (我们使用赋值运算符(=)为JavaScript变量赋值,它总是返回一个值。这对于大多数人来说是显而易见的,比如加法+或乘法。)

let x = 3 * 2 + 1;
console.log( x ); // 7

Remainder %

Remainder % (余数)

The remainder operator % does not have anything related to percents, the result of a % b is the remainder of the integer division of a by b. (余数运算符%没有任何与百分比相关的内容, a % b的结果是a除以b的整数的余数。)

console.log( 7 % 2 ); // 1 is a remainder of 7 divided by 3
console.log( 11 % 3 ); //  2 is a remainder of 11 divided by 3
console.log( 12 % 3 ); // 0 is a remainder of 12 divided by 4

Exponentiation **

Exponentiation ** (冪)

The exponentiation operator ** was recently added to the language. For a number b, the result of a ** b is a multiplied by itself b times:

console.log( 3 ** 2 ); // 9  (3 * 3)
console.log( 3 ** 3 ); // 27  (3 * 3 * 3)
console.log( 3 ** 4 ); // 81 (3 * 3 * 3 * 3)

The operator works also for non-integer numbers:

console.log( 9 ** (1/2) ); // 3 (power of 1/2 is the same as a square root)
console.log( 27 ** (1/3) ); // 3 (power of 1/3 is the same as a cubic root)

Increment/decrement

Increment/decrement (递增/递减)

Increasing or decreasing a number by 1 is one of the most common numerical operations. There are special operators for it:

let counter = 10;
counter++;  // it's a counter = counter + 1
// it will be shorter
console.log( counter ); // 11

Increment or decrement can only be applied to variables, so If we try to use it on a value like 6++ it will give an error. (增量或减量只能应用于变量,因此,如果我们尝试将其用于6 + +之类的值,则会产生错误。)

We can place the operators ++ and – before or after a variable. (我们可以将运算符+ +和–放在变量之前或之后。)

  • When the operator is after the variable, it is in “postfix form” : counter++.

  • When the operator is before the variable, it is in “prefix form” : ++counter.

These two statements do the same thing: increase counter by 1.

let counter = 1;
let a = ++counter; 
console.log(a); // 2

Increment/decrement among other operators

Increment/decrement among other operators (其他运算符中的增量/减量)

The operators ++/– can also be used inside expressions. Their priority is higher than most other arithmetical operations. (运算符+ +/–也可以在表达式内部使用。它们的优先级高于大多数其他算术运算。)

Let’s compare these two examples:

let counter = 2;
console.log( 2 * ++counter ); // 6

and (和)

let counter = 2;
console.log( 2 * counter++ ); // 4, because counter++ returns the "old" value

Technically they are okay, but this notation usually makes code less readable. One line does multiple things, what is not good. (从技术上讲,它们没问题,但这种符号通常会降低代码的可读性。一行做多件事,什么不好。)

During reading code, a fast “vertical” eye-scan can easily miss something like counter++ and it won’t be obvious that the variable increased. That’s why we advise a style of “one line – one action”:

let counter = 1;
console.log( 2 * counter );//2
counter++;
console.log( counter );//2


请遵守《互联网环境法规》文明发言,欢迎讨论问题
扫码反馈

扫一扫,反馈当前页面

咨询反馈
扫码关注
返回顶部