• 📢 hello everyone, I am Xiao Cheng, a student, recently in the brush little Red book, this is a study notes
  • 📢 wish you and I together in this wanton life big shine
  • This is the third day of reading advanced Programming in JavaScript (4th edition). The book has been read 82/865

Chapter 3 is really good, but some of it is very basic and a little boring

Chapter 3: Language Basics (Continued)

3.5 the operator

3.5.1 Unary Operators

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

1. The increment and decrement operator

Prefacing: ++(–) increments (decrement) the value before executing the statement

Posttype: value ++(–) execute statement with value first, then increment (decrement)

i++
++i
Copy the code

Note: The Boolean value ++ changes false to 1

When applied to an object, the object’s valueOf() method is first called to get an actionable value, and then the above rule is called on that value. If the result is a NaN, the previous rule is applied after the toString() method is called

2. Unary addition and subtraction operators

When the added value is non-numeric, the same type conversion is performed as with the Number() conversion function

Same thing with unary subtraction

let str = "ljc";
str = -str; //NaN
Copy the code

3.5.2 Bit operators

  1. Bitwise operators are used at the most basic level, manipulating numeric values in terms of the bits that represent them in memory.
  2. All values in JavaScript are stored in 64-bit format, but bitwise operators do not directly manipulate 64-bit values. Instead, you now convert the 64-bit value to a 32-bit integer, perform the operation, and finally convert the result back to 64-bit
  3. To calculate the binary complement of a number:
    1. The binary code for the absolute value of this value
    2. The binary inverse code, zero and one interchange
    3. The binary inverse plus 1
1. According to a non

With the bitwise non-operator (~), the result of performing bitwise is the inverse of the number

Or you could think of it as the negative of the operand minus 1

let num1 = 25;
let num2 = ~num1; / / - 26
Copy the code
2. Bitwise and

With the bitwise and operator (&), we operate on two numbers and compare the binary numbers so that the number of digits that are both 1 is 1

let result = 25 &  3;
// The result is 1
Copy the code

(3) or by location

Using the bitwise or operator (|), operating two Numbers, 1 is 1

let result = 25 | 3;
// The result is 27
Copy the code

4. Xor by bit

Use the bitwise xor operator (^) to operate on two numbers, only 1 if both of them are different

let result = 25 ^ 3;
// The result is 26
Copy the code

5. Shift to the left

Use the left shift operator (<<)

  • After moving to the left, the space on the right will be filled with 0
  • The left shift does not affect the sign bit of the operand

6. Move right with signs

The right shift operator (>>)

How do you understand that?

The way I think about it, if you move it to the right, and you put zeros on the left, you squeeze out the value on the right 32 times

7. Unsigned right shift

Unsigned right shift (>>>)

For negative numbers, it’s hard

Seems to be the value of the inverse code as the initial value before the right shift, and then the normal shift

3.5.3 Boolean operators

1. The logic

Logical non-operators (!)

  • ! NaN returns true
  • ! Null returns true
  • ! Undefined returns true
2. Logic and

The logic and operator (&&) operates on two values, only true if both are true

  • If the first operand is an object, the second operand is returned.

  • If the second operand is an object, the object is returned only if the first operand evaluates to true.

  • If both operands are objects, the second operand is returned.

  • If one of the operands is null, null is returned.

  • If one of the operands is NaN, then NaN is returned.

  • If one of the operands is undefined, undefined is returned.

3. The logical or

The Boolean or operator (| |), there is true is true

3.5.4 Multiplication operators

If one of the operands involved in the multiplication calculation is not a Number, the background first converts it to a Number using the Number() transformation function. The empty string is treated as 0, and the Boolean true is treated as 1

1. The multiplication (*)

multiplication

  • If Infinity is multiplied by 0, the result is NaN

  • If Infinity is multiplied by a non-zero value, the result is Infinity or -infinity, depending on the sign of the signed operand

2. The division (/)
  • If zero is divided by zero, the result is NaN

  • If Infinity is divided by any non-zero value, the result is Infinity or -infinity, depending on the sign of the signed operand

3. : (%)

Modulo operator

3.5.5 Exponential operator

Using Math. Pow

console.log(Math.pow(3.2); / / 9
Copy the code

3.5.6 The additive operator

1. The additive (+)
2. The subtraction (-)

3.5.7 Relational Operators

  • If both operands are strings, the encoding of the corresponding characters in the string is compared one by one
  • If any of the operands are objects, its valueOf() method is called, the result is obtained and the comparison is performed according to the previous rule.
  • If there is no valueOf() operator, the toString() method is called, the result is obtained and the comparison is performed according to the previous rule.
  • If any of the operands are Boolean values, they are converted to numeric values and then compared.
let result = "Brick" < "alphabet"; // true
Copy the code

3.5.8 Equality operator

1. Equals and does not equal

2. Congruent and incongruent

Determining congruent also compares data types for equality

let result2 = ("55"= = =55); // false, not equal, because the data type is different
Copy the code

3.5.9 Conditional Operators

Is the ternary operator (? : 🙂

let variable = boolean_expression ? true_value : false_value;
Copy the code

When the first expression is true, the value of variable is true_value; if false, false_value

3.5.10 The assignment operator

  • Multiply and assign (*=)

  • Divide and assign (/=)

  • Modulus (%=)

  • Add and assign (+=)

  • Subtraction assignment (-=)

  • Assign after left shift (<<=)

  • Assign after right shift (>>=)

  • Assignment after unsigned right shift (>>>=)

3.5.11 The comma operator

This is something you don’t really use but some bad guys have it in their interview questions

The comma operator always returns the last item in an expression

let num = (5.1.4.8.0); // The num value is 0
Copy the code

3.6 the statement

3.6.1 track if statement

if (condition) statement1 else statement2
Copy the code

3.6.2 do – while statement

The code in the loop body is executed at least once

// The loop repeats as long as I is less than 10. I starts at 0 and increments by 1.
let i = 0;
do {
	i += 1;
} while (i < 10);
Copy the code

3.6.3 while statement

// The variable I starts at 0 and increments by 1 each time. As long as I is less than 10, the loop continues
let i = 0;
while (i < 10) {
	i += 1;
}
Copy the code

3.6.4 radar echoes captured for statement

The code is tested and initialized before entering the loop

let count = 10;
for (let i = 0; i < count; i++) {
	console.log(i);
}
Copy the code

Initialization, conditional expressions, and post-loop expressions are not necessary

for (;;) { // Infinite loop
	doSomething();
}
Copy the code

3.6.5 the for in statement

1) Iterate through all the array elements in the array

2) Iterate over all the properties of the JavaScript object

for (index in object)
{
    statement
}
Copy the code

As the array is traversed, the loop counter for the for in loop is the index value of the array element

3.6.6 for – statement

The essential difference from for-in is that for-of traverses the value of the property of an array or object

let arr = [1.'2'.null.3.undefined.'4'];
for(let i of arr){
    console.log(i);
}
// 1 2 null 3 undefined 4
Copy the code

3.6.7 Label statements

Add a start tag to the for statement and jump to the specified tag with the break statement

start: for (let i = 0; i < count; i++) { 
 console.log(i); 
}
Copy the code

3.6.8 Break and continue statements

outermost:for() {}break outermost;
Copy the code

So that’s basically how you do it

Other uses are the same

The 3.6.9 with statement

The purpose of the with statement is to scope the code to a specific object

let qs = location.search.substring(1); 
let hostName = location.hostname; 
let url = location.href;
Copy the code

Is equivalent to

with(location) { 
 let qs = search.substring(1); 
 let hostName = hostname; 
 let url = href; 
}
Copy the code

It’s not recommended because it’s a little less code

3.6.10 switch statement

The switch statement can simplify complex if statements

Put a look, look at the grammar

let num = 25;
switch (true) {
	case num < 0:
		console.log("Less than 0.");
		break;
	case num >= 0 && num <= 10:
		console.log("Between 0 and 10.");
		break;
	case num > 10 && num <= 20:
		console.log("Between 10 and 20.");
		break;
	default:
		console.log("More than 20.");
}

Copy the code

The switch statement uses the congruent operator when comparing the values of each condition, so it does not cast the data type (for example, if the string “10” is not equal to the number 10).

3.7 the function

The functions here are just simple usage, there’s nothing to say, there’s a topic in Chapter 10, right

Functions that do not specify a return value actually return the special value undefined.