Operators in Ja...LOGIN

Operators in JavaScript

JS operators

To perform various operations, different operation symbols must be used.


## Arithmetic operators: +, -, *, /, %, ++, --

A = 10 + 20;

A = 10 – 20;

A = 10 * 20;

A = 10 / 20;

(1) "%" remainder operator, divide two numbers and get the remainder.

A = 10 % 3; // A = 1, if the remainder is not 0, then the two are not divisible

A = 10 % 2; // A = 0, If the remainder is 0, then the two numbers can be divided


(2) "++" plus 1 operator, self-increment 1

"++" can be used as a prefix (++i) or as a suffix (i++).

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>php.cn</title>
        <script>
           var a = 1;
           var b = 1;
           document.write(++a);
           document.write("<hr>")
           document.write(b++);
        </script>
    </head>
    <body>
    </body>
</html>

Observe the output of the above example

  • When ++a, no matter what, a=a+ will be executed first 1. Then perform output and other operations, such as page output and calculations.

  • When b++ is used, other operations, such as output and calculations, will be performed first. Class, finally when this statement ends, execute a=a+1, and then this statement ends

(3) "--" minus 1 Operator, decrement by 1

"--" can be used as a prefix (--i) or as a suffix (i--).

The "--" example is the same as the "++" example. Please try and test it yourself.


Assignment operators: =, +=, -=, *=, /=

“+= "Add first and wait later. For example: a += 10 //After expansion, a = a + 10

"-=" first subtract and then wait. For example: a -= 10 //After expansion, a = a - 10

"*=" multiply first and then wait. For example: a *= 10 //After expansion, a = a * 10

"/=" divide first and then wait. For example: a /= 10 //After expansion, a = a / 10


##String operators: +, + =

#Strings can only perform "connection" operations and cannot perform other operations.

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>php.cn</title>
        <script>
           var name = "php.cn";
           var str = "欢迎来到"+name;
           document.write(str);
        </script>
    </head>
    <body>
    </body>
</html>

Comparison operators: >, <, >=, <=, ==, !=, ===, !==

The result of the comparison operator is a Boolean value (true or false).

A = 10 > 20; // Result A=false

A = 20>=20; // Result A=true

A = 10 %2 == 0; // Result A=true

A = 10%2 == “0”; // Result A=true

A = 10%3 != 0; // Result A=true

A = 10%2 === “0”; //Result A=false

Note:


"=" is the assignment number. For example: a = 10

"==" equals. Only the values ​​of two variables are compared, regardless of type. As long as the values ​​are the same, it returns true, otherwise it returns false.

"===" are all equal. Both compare variables and determine types. If the type and value are the same, return true, otherwise return false.

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>php.cn</title>
        <script>
        //比较字符串数值和数值
           var name1 = "520";
           var name2 = 520;
        document.write(name1==name2);
        document.write("<hr>");
        document.write(name1===name2)
        </script>
    </head>
    <body>
    </body>
</html>


Logical operators: &&, ||, !

The logical operator has two results: true or false.

"&&" logical AND (and relationship). If both left and right operands are true, the result is true, otherwise, the result is false.

Logical AND means that when two conditions are met at the same time, the result is true.

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>php.cn</title>
        <script>
        //给一个成绩
        var score=61;
        //判断成绩所属级别
        if(score<60){
            document.write("对不起,您没有及格");
        }else if (score>=60&&score<70){
            document.write("您刚好及格");
        }
        </script>
    </head>
    <body>
    </body>
</html>

"||" logical OR. As long as one of the two conditions on the left and right is met, true is returned, otherwise false is returned.

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>php.cn</title>
        <script>
        var age=79;
        if(age<10||age>60){
            document.write("您好,您符合我们店的优惠条件,今天买东西全场5折");
        }else if (age>=10&&age<=60){
            document.write("不好意思,您不符合我们店的优惠条件,今天买东西不享受折扣");
        }
        </script>
    </head>
    <body>
    </body>
</html>

"!" negation operation. !true = false , !false = true , !100 = false

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>php.cn</title>
        <script>
        var a=true;
        document.write(a);
        document.write("<br/>");
        document.write(!a);
        </script>
    </head>
    <body>
    </body>
</html>

Ternary operator: ?:

The so-called "ternary operator" refers to three operands.

Syntax: Conditional expression? Result 1: Result 2

Syntax: Operand 1? Operand 2: Operand 3

Meaning: If the condition is true, then Execute the code for "Result 1"; if the condition is false, execute the code for "Result 2".

Actually: the ternary operator is the modified form of if else. (if else we will study in the next chapter)

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>php.cn</title>
        <script>
        var a=10;
        var b=20;
        //判断a和b那个大,把大的那个赋值给max
        var max=a>b?a:b;
        document.write("最大值:"+max);
        </script>
    </head>
    <body>
    </body>
</html>


Next Section
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>php.cn</title> <script> var a = 1; var b = 1; document.write(++a); document.write("<hr>") document.write(b++); </script> </head> <body> </body> </html>
submitReset Code
ChapterCourseware