Javascript operator (js operator)

Table of contents

1. Arithmetic operators

2. Increment and decrement operators

3. Comparison operators

4. Logical operators

5. Assignment operator

6. Operator precedence

7. Process control

8. Ternary expressions


operator

Operators, also known as operators, are symbols used to implement functions such as assignment, comparison, and arithmetic operations. Common operators: arithmetic operators, increment and decrement operators, comparison operators, logical operators, assignment operations symbol.

1: arithmetic operator

Concept: A symbol used in arithmetic operations to perform arithmetic operations on two variables or values.

Add (+), subtract (-), multiply (*), divide (/), take remainder (%)

Remainder (%): Returns the remainder of the division.

Note: The precision of floating point numbers

Floating-point values ​​have a maximum precision of 17 decimal places, but are far less precise than integers when performing arithmetic calculations.

var result = 0.1 + 0.2; // The result is not 0.3, but: 0.30000000000000004

console.log(0.07 * 100); // The result is not 7, but: 7.000000000000001

So: don't directly test whether two floating-point numbers are equal!

2: Increment and decrement operators

If you need to repeatedly add or subtract 1 to a numeric variable, you can do so using the increment ( ++ ) and decrement ( -- ) operators.

Example:

var num=1;num++;//2

var num=1;++num;//2

When placed in front of a variable, we can call it a pre-increment (decrement) operator, and when it is placed after a variable, we can call it a post-increment (decrement) operator.

Pre-increment:

++num pre-increment, that is, self-increment by 1, similar to num = num + 1, abbreviated ++num, pre-increment, self-increment first, and then return the value.

  var num = 10;

  alert(++num + 10);   // 21

post-increment:

num++ is self-increment by 1, similar to num = num + 1, abbreviated as num++, post- increment, return to the original value first, and then self-increment

  var num = 10;

  alert(10 + num++);  // 20

practise:

var a = 10;

 ++a;

var b = ++a + 2;

console.log(b);

var c = 10;

c++;

var d = c++ + 2;

console.log(d);

var e = 10;

var f = e++ + ++e;

console.log(f);

3: Comparison operators

< less than sign 1<2 true

> greater than sign 1>2 false

>= greater than or equal to 2>=2 true

<= less than or equal to 3<=2 false

== is equal 2==2 true

!= inequality sign 3!=2 true

=== Full equal sign The value and data type must be equal Example: "3"===3 false, "3"==3 true

Example:

console.log(3 >= 5); //false

console.log(2 <= 4); //true

console.log(3 == 5);//false

 console.log('Chinese' == 'chinese'); //false

console.log(10 == 10); //true

 console.log(10 == '10');//true

  console.log(10 != 10); //false

 console.log(10 === 10);//true

 console.log(10 === '10'); //false

4: Logical operators

Logical operators are operators used to perform operations on Boolean values, and their return values ​​are also Boolean values.

- Logical AND &&

Return true if both sides are true, otherwise return false

console.log(true && false) //false;

Console.log(3>2 && 1>2); //false

- logical OR ||

As long as one side is true, it returns true, and when both sides are false, it returns false

console.log(true ||  false) //true;

Console.log(3>2 ||  1>2); //true

- 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 isOk = !true;

 console.log(isOk);  // false

practise:

var num = 7;

var str = "I love you~China~";

console.log(num > 5 && str.length >= num);

console.log(num < 5 && str.length >= num);

console.log(!(num < 10));

console.log(!(num < 10 || str.length == num));

- short-circuit operation (logic interrupt)

The principle of short-circuit operation: when there are multiple expressions (values), when the value of the expression on the left can determine the result, the value of the expression on the right will not continue to be calculated;

console.log( 123 && 456 );        // 456

console.log( 0 && 456 );          // 0

console.log( 123 && 456&& 789 );  // 789

console.log( 123 || 456 );         //  123

console.log( 0 ||  456 );          //  456

console.log( 123 || 456 || 789 );  //  123

practise:

var num = 0;

console.log(123 || num++);

console.log(num);

var num = 0;

console.log(123 && num++);

console.log(num);

5: Assignment operator

= direct assignment var realname="Xiaoming";

+=, -=, *=, /=, %= are calculated first before assignment;

Example:

var age = 10; age+=5;//15

var age = 10; age-=5;//5

var age = 10; age*=5;//50

var age = 10; age/=5;//2

var age = 10; age%=5;//0

practise:

var num = 10;

num += 5;

console.log(num);

var age = 2;

age *= 3;

console.log(age);

6: Operator precedence

1: Parentheses ()

2: Unary operators ++, --, !

3: Arithmetic operators *, /, % first, then +, -

4: Relational operators >, >=, <, <=

5: Equality operators ==, !=, ===, !==

6: Logical operator first && then ||

7: Assignment operator =

8: comma operator ,

practise:

- console.log(10 >= 11 || '中文' != 'chinese' && !(1 0  * 10  == 150 )

- var num = 10;

 console.log(5 == num / 2 && (2 + 2 * num).toString() === '22');

- var a = 10 > 5 && 5 < 10 && 5 == 10;

 console.log(a);

-  var b = 5 <=10 || 10 > 5 || 10 != 5;

console.log(b);

7: Process control

Definition: It is to control the code to be executed according to a certain structural order. There are mainly three types of process structures: sequential structure, branch structure, and loop structure.

sequential mechanism

The most basic flow control, it has no specific grammatical structure, and the program will be executed in the order of the code.

branch structure

In the process of executing code from top to bottom, different path codes are executed according to different conditions, so as to obtain different results. JS branch structure is divided into two types: if statement and switch statement.

- if statement

If(conditional expression){

   // code to execute if the condition is true

}

If(conditional expression){

   // Execute code if the condition is true

}else{

  // If the condition is not true execute this code

}

// Good for checking multiple conditions.

if (conditional expression 1) {

       Execute code 1;

} else if (conditional expression 2) {

         Execute code 2;

} else if (conditional expression 3) {

       Execute code 3;

 } else {

        // None of the above conditions are true to execute the code here

 }

Example:

If( ''chinese' != 'chinese' ){

     //execute code

}

var sex = prompt('请输入您的性别:');

if (sex==”男”) {

     alert('请上男卫生间');

}else if(sex==”女” ){

      alert('请上女卫生间');

}else{

    alert(“?”);

}

    

- switch statement

switch(expression) {

     case n:

        code block

        break;

     case n:

        code block

        break;

     default:

        default code block

}

Example:

var sex=1;

switch (sex){

    case 1:

console.log("您喜欢的是猫");

break;



case "狗":

console.log("您喜欢的是狗");

break;



default:

console.log("其他动物");

}

Summarize:

- We develop expressions inside which we often write as variables

- The value of our num and the value in the case must be congruent

- break If there is no break in the current case, it will continue to execute the next case

8: Ternary expressions

Syntax structure: expression1 ? expression2 : expression3;

Example:

var num=11;

var res = num >10 ? 'Yes' : 'No';

Console.log(res); //No

Guess you like

Origin blog.csdn.net/weixin_65607135/article/details/126997466