JavaScript Basics - Operators (3)

JavaScript Basics - Operators (3)



1. What is an operator

Operators, also known as operators, are symbols used to implement functions such as assignment, comparison, and arithmetic operations.

2. Expressions and return values

An expression is a combination of numbers, operators, variables, etc. that can obtain values ​​and meaningful arrangements. Generally speaking, it is a formula composed of numbers, variables, and operators. For example: 1+1=2, 100-100 = 0.
From the above example, we know that the expression will have a result, which is returned to us, we call it the return value

// 在程序中我们往往是把返回值赋给变量
var sum = 12 + 34;  // 右边计算得到返回值赋给左边的变量
console.log(sum); // 结果为 46

3. Overview of Arithmetic Operators

Arithmetic operators are used to perform arithmetic operations on two variables. details as follows:
insert image description here

  1. Addition, subtraction, multiplication, and division are similar to mathematical operations. The operator = is used to assign values ​​​​to js variables:
var x = 11; // 把 12 赋值给 x 变量
var y = 22;
var num1 = x + y; // 把两个变量的值相加
var num2 = x - y; // 两数相减
var num3 = x * y; // 两数相乘
var num4 = x / y; // 两数相除
console.log(num1); // 结果为 33
console.log(num2); // -11
console.log(num3); // 242
console.log(num4); // 0.5

Note: When calculating floating-point numbers, the output will be problematic, because the highest precision of floating-point numbers is 17 decimal places:

var num1 = 0.1;
var num2 = 0.2;
var sum = num1 + num2;
console.log(sum); // 输出结果为 0.30000000000000004
// 所以两个浮点数不能进行比较是否相等
  1. The remainder symbol is the same as the division sign in mathematics, but the two are just similar in appearance, but their functions are quite different. The remainder is to divide the two, take the remainder, and the remainder is always consistent with the sign of the dividend. The specific code is as follows:
var num1 = 15;
var num2 = 3;
var num3 = -25;
var num4 = 7;
var x = num1 % num2;
var y = num3 % num4;
console.log(x); // 0
console.log(y); // -4

Note: If the dividend is NaN, then the result is NaN.

var result = NaN % 2;
console.log(result); // 结果为 NaN

4. Assignment operator

Assignment operators are used to assign values ​​to JavaScript variables, as follows:
insert image description here

// = 是直接赋值
var num = 15;
// +=、-=、*=、/=、%=是先进行运算再赋值
num += 1; // 相当于 num = num + 1
num -= 1; // 相当于 num = num - 1
num *= 10; // 相当于 num = num * 10

5. Increment and Decrement Operators

5.1 Overview of increment and decrement operators

To repeatedly add or subtract one to a numeric variable, you can use the increment operator ( ++ ) and the decrement operator ( ). In js, the increment and decrement operators can be placed in front of the variable or in the After the variable, note that it must be used in conjunction with the variable:

  • When placed in front of a variable, we call it a pre-increment (decrement) operator

  • After the variable is what we call the post-increment (decrement) operator

5.2 Increment operator

5.2.1 Pre-increment operator

The pre-increment operator is written in front of the variable, such as: ++num, which means self-increment, which is equivalent to num = num + 1, but ++num is simpler.

var age = 18;
++age; // 相当于 age = age + 1

5.2.2 Post-increment operator

The post-increment operator is written after the variable, which also means self-increment, and the writing method is simpler. It returns the original value first and then self-increment.

var age = 20;
age++; // 相当于 age = age + 1

5.2.3 Difference between Postfix and Prefix Operators

If the two are used alone, the effect is the same, but there is a little difference in the calculation process. When used with other codes, the execution results will be different. The preposition is to return the value after the self-increment, and the postposition is to return the value first, and then Add from:

// 前置递增运算符
var num = 10;
console.log(++num + 1); // 结果是12,因为num先自加一变11,返回,再加1
// 后置递增运算符
var age = 15;
console.log(age++ + 5); // 结果是20,因为后置先返回原值,再加1
console.log(age); // 结果是16,是自加的结果

Note: Post-increment (decrement) operators are generally used in development.

6. Comparison Operators

Comparison operators are used for comparison between two data, and the operation result is a Boolean value
insert image description here

console.log(2 == 4); // false
console.log(54 <= 134); // true
console.log(19 === '19'); // false, 全等符号要求两侧的值和数据类型都一模一样,才为真(true)

7. Logical operators

7.1 Overview of Logical Operators

Logical operators are mainly used to perform Boolean operations, and their return values ​​are Boolean values, which are mainly used for conditional judgment:
insert image description here

7.2 Logical AND

&& (logical AND) returns true if both sides are true, otherwise returns false

var result = 2 > 1 && 5 > 3; // true
var outcome = 10 > 100 && 4 > 1; // false

7.3 Logical OR

|| (logical OR) returns true as long as one of them is true, and returns false if both sides are false

var result = 34 > 12 || 12 > 34; // true
var outcome = 23 < 12 || 56 < 45; // false

7.4 Logical NOT

Logical not ( ! ) is also called negation, which is used to take the opposite value of a Boolean value, such as the opposite value of true is false

var sayOk = !true;
console.log(sayOk); // false

7.5 Logic and short-circuit operation (logic interrupt)

Short-circuit operation : When there are multiple expressions (values), the value of the expression on the left can be determined, and the expression on the right will not be evaluated.

Logical and short-circuit operations :

  • Syntax: expression1 && expression2
  • If expression 1 is true, return expression 2 (true return other)
  • If expression1 is false, return expression1 (return from false from)
console.log(0 && 23); // 结果为0,因为0为假,返回0本身
console.log(123 && 356); // 结果为356,因为123为真,返回356 

7.6 Logical or short-circuit operation

  • Syntax: expression1 || expression2
  • If expression1 is true, return expression1 (return true from)
  • If expression 1 is false, return expression 2 (return other from false)
console.log(123 && 345); // 结果为123
console.log(0 && 467); // 467

7.7 Application of short-circuit calculation

Logical interruption is very important, it will directly affect the running results of the program:

var age = 18;
console.log(345 || age++); // 345为真,返回,不执行age++
console.log(age); // 值还是18,因为age++没有执行

8. Operator precedence

insert image description here

  • Logical AND has a higher priority than logical OR

  • Logical non- precedence in unary operators is high

  • Higher priority first, then lower priority

  • Equal priority is counted from left to right

Guess you like

Origin blog.csdn.net/EXIxiaozhou/article/details/128292269