New JavaScript operator (day10)

catalogue

1. Operator

2. Arithmetic operator

2.1 concept of arithmetic operator

2.3 problem summary

2.4 expressions and return values

3. Increment and decrement operators

3.1 overview of increment and decrement operators

3.2 increment operator

3.3 summary of pre increment and post increment

4. Comparison operator

4.1 comparison operator

4.2 = summary

1. Operator

Operators, also known as operator s, are symbols used to assign values, compare and perform arithmetic operations.

Common operators in JavaScript are:

Arithmetic operator

Increment decrement operator

Comparison operator

Logical operator

Assignment Operators

2. Arithmetic operator

2.1 concept of arithmetic operator

Concept: the symbol used in arithmetic operation is used to perform the arithmetic operation of two variables or values.

operatordescribeexample
+

plus

10+20=30
-reduce10-20=-10
*ride10*20=200
/except10/20=0.5
%Remainder (modulus)Returns the remainder of division 9% 2 = 1

Here are some simple operations

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    console.log(1 + 1);//2
    console.log(1 - 1);//0
    console.log(1 * 1);//1
    console.log(1 / 1);//1
    //1.% remainder
    console.log(4 % 2);//0
    console.log(5 % 3);//2
    console.log(3 % 5);//3
    //2. There will be problems in floating-point arithmetic
    console.log(0.1 + 0.2 );//0.30000000000000004
    //We can't directly compare whether the floating-point numbers are equal

    var num = 0.1 + 0.2;
    console.log(num == 0.3);//false
</script>
</body>
</html>

The highest precision of floating-point values is 17 decimal places, but its precision is far less than that of integers.

So: don't judge whether two floating-point numbers are equal.

2.3 problem summary

Q: how do we judge whether a number can be divisible?

  A: its remainder is 0, which means that this number can be divided. This is the main purpose of the% remainder operator.

2. Q: what is the result of 1 + 2 * 3?

    A: note that arithmetic operators also have priority. Multiply and divide first, then add and subtract. If there are parentheses, calculate the ones in parentheses first.

2.4 expressions and return values

Expression: it is a combination of numbers, operators, variables, etc. with meaningful arrangement methods that can obtain numerical values

Simple understanding: it is a formula composed of numbers, operators, variables, etc

The expression will eventually have a result returned to us, which we call the return value

console.log(1 + 1);//2 is the return value
//In our program, 2 = 1 + 1 calculates the expression on our right and returns the value to the left
var num = 1 + 1;

3. Increment and decrement operators

3.1 overview of increment and decrement operators

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

In JavaScript, increment (+ +) and decrement (- -) can be placed in front of or behind a variable. When placed in front of a variable, we can call it a pre increment (decrement) operator, and when placed behind a variable, we can call it a post increment (decrement) operator.

Note: increment and decrement operators must be used with variables.

3.2 increment operator

Prepositive operator

++The increment before num is self adding 1, which is similar to num = num + 1, but + + num is easier to write.

//2. Preceding increment operator + + unloading variables
var age = 10;
++age;//Similar to age = age + 1
var p = 10;
console.log(++p + 10)//21

Use the formula: first add 1, and then return the value.

Post increment operator

Num + + Post increment is self adding 1, which is similar to num = num + 1, but num + + is easier to write.

Use the formula: return the value first, and then add it by yourself

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    var num = 10;
    num ++; //num num + 1
    console.log(num);
    //1. If the pre auto increment and post auto increment are used separately, the effect is the same
    //2. Post auto increment formula: first return the original value and then add 1
    var age = 10;
    console.log(age++ + 10);//20 return the original value first
    console.log(age);//11
</script>
</body>
</html>

3.3 summary of pre increment and post increment

The pre increment and post increment operators can simplify the writing of code and make the value of variable + 1 easier than before

The results are the same when used alone.

When combined with other code, the execution result will be different

Post: original value operation before self addition (others before yourself)

Pre: self addition before operation (self before others)

During development, most of them use post increment / decrement, and the code is exclusive on one line, such as num + +; Or num --;

4. Comparison operator

4.1 comparison operator

Concept: comparison operator (relational operator) is the operator used when comparing two data. After the comparison operation, a Boolean value (true/false) will be returned as the result of the comparison operation.

Operator nameexplaincaseresult
<Less than sign1 < 2true
>Greater than sign1 > 2false
>=Greater than or equal to sign (greater than or equal to)2 >= 2true
<=Less than or equal to sign (less than or equal to)3 <= 2false
==Equal sign (transformation)37 == 37true
!=Unequal sign37 != 37false
===        !==Congruent     Required values and     Data types are always37==='37'false
console.log(3 >= 5); //false
console.log(2 <=4 ); //true
//1. The equal sign in our program is = = the default conversion data type will convert string data to numeric data, as long as the evaluation is equal
console.log(3 == 5);
console.log('Cheng xuape' == 'Cheng Xuyuan');//false
console.log(18 == 18);//ture
console.log(18 =='18');//true
console.log(18 != 18);//false
//2. we have as like as two peas in the program, and we need to have the same values and data types on both sides to make true.
console.log(18 === 18);//true
console.log(18 ==='18');//false

4.2 = summary

Symboleffectusage
=assignmentGive the right to the left
==judgeWhether the values on both sides of the half segment are equal (note that there is implicit conversion at this time)
===CongruentJudge whether the values and data types on both sides are exactly equal

Tags: Javascript ECMAScript

Posted on Sun, 05 Dec 2021 16:35:05 -0500 by freelancedeve