Note: This article is part 2 of chapter 3 of the “Relearning JavaScript Advanced Programming” series.
About “relearning JavaScript advanced programming” is a review of the basics of JS learning.

1. The operator

The concepts used to describe a set of data values that can be manipulated are called operators. These include arithmetic operators (plus and minus), bitwise operators, relational operators, and equality operators. The ES operator differs from others in that it can be applied to many values, such as strings, numeric values, booleans, and even objects. When applied to an object, you call the valueOf() and toString() methods of the object.

1.1 Unary operators

Operators that operate on only one value are called unary operators.

1.1.1 Increment and decrement operators

There are two types: pre and post, with the pre before the variable to be manipulated. The postfix comes after the variable to be manipulated. As follows:

Var age = 29 ++age // 30 = age = age + 1 --age // 28 = age = age-1Copy the code

Note: When pre-incrementing or decrement is performed, the value of the variable is changed before the statement is evaluated.

Var age = 29 var a = --age + 2 age // 28 a // 30 // var num1 = 2 var num2 = 20 var num3 = --num1 + num2 // 21 var num4 = num1 + ++num3 // 23Copy the code

The syntax of the post-increment and decrement operations remains the same, except that they are placed behind, and most importantly, the post-increment and decrement operations are performed after the variable executes. As follows:

var num = 3
var age = 4

num-- + age-- // 7
num // 2
age // 4
Copy the code

Conclusion:

The above four operators apply to any value, not just numbers. They can also be applied to strings, bores, floating-point values, and objects as follows: 1. When applied to numeric strings, convert them to numeric values.

2, when applied to the string does not contain valid number, set the value of the variable NaN, string into a numerical variables 3, used in a Boolean false, converts it to 0, add and subtract 1 in the implementation of the operation, the Boolean value into number 4, used for Boolean value of true, convert it to 1, plus or minus 1 in the implementation of the operations, 6. For objects, the valueOf() method of the object is called first. If the result is that NaN is calling toString(), the variable of the object becomes a numeric variable.Copy the code

The above instructions are as follows:

Var s1 = '2' var s2 = 'z' var s3 = false var s4 = 1.1 var s5 = {valueOf: Function () {return - 1}} s1 + + / / 3 s2 + + + + / / NaN s3 / s4/1-0.10000000000009 / / s5 - / / - 2Copy the code

1.1.2 Unary addition and subtraction operators

The unary plus operator does nothing before a variable, while the unary minus operator does nothing before a variable

var num = 25
num = +num // 25

num = -num // -25
Copy the code

Note: Following the previous rule, using the addition and subtraction operators on string variables becomes NaN and the rest becomes the value of the specified rule.

The unary addition and subtraction operators are used primarily for basic arithmetic operations and can also be used to convert data types.

1.2 bit operators

Bitwise operators are bits used to represent values in memory to manipulate values. All values in ES are stored in 64-bit format, but the bit operators do not directly manipulate 64-bit values. Instead, they first convert 64-bit values to 32-bit integers, then perform operations, and finally convert the result to 64-bit.

For signed integers, the first 31 of the 32 bits are used to represent the value of the integer, the 32nd bit represents the symbol of the number, 0 represents the integer, and 1 represents a negative number. This symbol bit is called the sign bit. The value of the sign bit determines the format of the other bit values. The positive numbers are stored in pure binary format, with each of the 31 bits representing a power of two, the first representing a power of two to the zero and so on. The ones that aren’t used are filled with zeros and ignored. Which is binary notation.

Negative numbers can also be stored in binary, but in the form of binary complement. The procedure for calculating the binary complement of a value is as follows:

1, find the binary code of the absolute value of this value 2, find the binary inverse code 3, get the binary inverse code +1 as follows: 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1110 1110Copy the code

In ES, ES tries to hide all of this information from us, that is, when we print a negative number as a binary string, all we see is that the absolute value of the negative number is preceded by a negative sign, as follows:

var num = -18
num.toString(2) // '-10010'
Copy the code

Note: by default, all the integers in the ES are signed integer, of course there are unsigned integer, for unsigned integer, 32nd symbol no longer, because the unsigned integer can be a positive number, and unsigned integer values can be bigger, because the extra a symbol no longer, can be used to show the value.

When a bit operator is applied to a value in ES, the following conversion takes place in the background: a 64-bit value is converted to a 32-bit value, then a bit operation is performed, and finally a 32-bit value is converted back to a 64-bit value. This may appear to be manipulating 32-bit values, but the other problem is that when bits are applied to special values such as NaN and Infinity, they are treated as zeros.

Note: If the bitwise operator is applied to a non-numeric value, the value is first converted to a numeric value using the Number() function. When applied to the bitwise operation, the result is a numeric value.

  1. Bitwise NOT

The bitwise nonoperator is represented by a wavy line (~), and the result of executing the bitwise nonoperator is to return the inverse of the value and subtract one, as follows:

Var num = 25 // binary 0001 1001 var num1 = ~num // binary 1110 0110 // -26Copy the code
  1. Bitwise and

The bitwise and operator is represented by an and character (&), which has two operator numbers. In essence, bitwise AND operations align each bit of two numbers, AND perform AND on two numbers in the same position according to the following rules:

Result: all 1s are 1, 0 is returned

The bits of the first number The bits of the second value The results of
1 1 1
1 0 0
0 1 0
0 0 0
  1. The bitwise OR (OR)

Bitwise or (|) by a vertical bar symbols, said there are also two operands, operation results follow the table below.

The bits of the first number The bits of the second value The results of
1 1 1
1 0 1
0 1 1
0 0 0

Result: 1 returns 1, all 0 returns 0

  1. Bitwise XOR

The bitwise xor operator is represented by a caret (^), also two operands, and the result follows the following table:

The bits of the first number The bits of the second value The results of
1 1 0
1 0 1
0 1 1
0 0 0

Result: Returns 1 if only one of two numbers is 1, or 0 if both numbers are 1 or 0

  1. Shift to the left

The left-shift operator, represented by two less-than signs (<<), moves all the bits of a numeric value to the left by the specified number of digits. As follows:

10 var newnum = oldnum << 5 // binary 1000000, decimal 64 note: After a shift to the left, there are five more Spaces to the right of the original value, which are filled with zeros so that the result is a full 32-bit binary number.Copy the code
  1. Signed shift to the right

The signed right-shift operator, represented by two greater than signs (>>), moves the value to the right but preserves the sign bit (that is, the sign sign). The signed right-shift operation is the opposite of the left-shift operation, which changes the result back to 2 if you move 64 five places to the right. As follows:

Var oldnum = 64 // binary 1000000 var newnum = oldnum In the process of the shift, the original value will also have a vacancy, but this time the vacancy appears to the left of the original value, the right of the sign bit, at this time ES will fill all the vacancy with the value of the sign bit, in order to get a complete value.Copy the code
  1. Unsigned shift to the right

The unsigned right-shift operator consists of three greater than signs (>>>) to the right. This operator moves all 32 bits of the value to the right. For positive numbers, an unsigned right-shift results in the same result as a signed right-shift, as shown below

Var oldnum = 64 // binary 1000000 var newnum = oldnum >>> 5 // Binary 10 decimal 2Copy the code

Note: the situation is different with negative numbers. First, an unsigned right shift fills the space with 0, not with sign bits as with a signed right shift. So, an unsigned right shift for a positive number gives the same result as a signed right shift, but not for a negative number. Second, the unsigned right-shift operator treats a negative binary as a positive binary. Moreover, since negative numbers are represented as their absolute complement, the result of an unsigned right shift is very large. As follows:

Var oldnum = 11111111111111111111111111000000-64 / / binary var newnum = oldnum > > > 5 / / decimal 134217726Copy the code

Welcome to pay attention to the public account [Xiaoyao students]

Re-learn js series

Relearn JS JavaScript introduction

Relearn JS to use JavaScript in HTML

Data types => Data types

ES6 Introduction series

ES6 Introduction to let and cont

ES6 introduction to variable deconstruction assignment

ES6 starter string extension

ES6 – An extension to get started with re

ES6 introduction to numerical extension

Extension of ES6 introductory functions

ES6 introduction to the array extension

Extensions to ES6 starter objects

Git tutorial

Front-end Git basics tutorial