Home >Web Front-end >JS Tutorial >What are the js operators? Detailed introduction to js operators

What are the js operators? Detailed introduction to js operators

不言
不言forward
2018-10-23 15:52:204382browse

This article brings you what are the js operators? The detailed introduction of js operators has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

javascript basic operators are divided into: arithmetic operators, assignment operators, relational operators, logical operators; the following is a detailed explanation of how to master several operators;

(1) Arithmetic operators

1. Arithmetic operators can be divided into unary operators, binary operators, and ternary operators according to the different operands on both sides of the operator;

2. According to different functions, it is divided into: (plus sign), — (minus sign), (multiplication sign), / (division sign) and % (remainder operation);

1-1 .Unary operator;

(auto-increment), - - (auto-decrement);

When the operator is followed by: when assignment does not occur, the increment and self-decrement operation is performed; assignment occurs When , assign the value first and then perform the operation.

When the operator is prepended: When no assignment occurs, the increment and self-decrement operation is performed; when an assignment occurs, the operation must be performed first and then the value is assigned.

Examples of operator postpositions;

var a=3;
a++;
consonle.log(a)//值为4
 var a=3;
    var b=4;
     a--;
     b--;     
    var c=a+b;//2+3
    console.log(c);//5
var a=3;
var b=4;
a++;
b++;
var c=(a+b);//4+5
consonloe.log(c);//c的值为9
   var a=3;
    var b=4;
    var c=(a++)+a+(b++)+b;//3+4+4+5
    console.log(c);//16
 var a=3;
    var b=4; 
    var c=(a--)+a+(a++)+(b++)+b+(b--);//3+2+2+4+5+5
    console.log(c);//21

Examples of operator prepositions;

 var b=4;
 --a;//2
 ++b;//5
 console.log(a,b);//  2  5
 var a=3;
 var b=4;
 var c=--a+b;//2+4  运算符前置 先运算 再赋值。
 console.log(c);//6
 var a=3;
 var b=4;
 var c=--a+--b;//2+3 
 console.log(c);//5

Comprehensive use of pre and postpositions;

    var a=3;
     var b=4;
     var c=(--a)+(--b)+(b++);//2+3+3 
     console.log(c);//8

Let’s explain it here: Let’s first Operate it separately;

(--a)=2; Operator is prefixed, operate first and then assign value;

(--b)=3: Same as (--a);

(b)=3; Many students will calculate it as equal to 4 or 5. We need to understand (--b) in this way and convert the value of b to 3, so (b) in brackets b is actually equal to 3 (b) is indeed equal to 4 after operation, but it is clear that it is the operator postposition, so when it is added to other values, its value is 3.

Let’s look at the next example;

     var a=5;
     var b=6;
     var c=(a++)+(b--)+(--a)-(--b);//5+6+5-4
     console.log(c);//12

(a) 5

(b--) 6 These two are operator postfixes, so they still have their own values;

(--a) 5 Because the value of (a) has been calculated before, it is 6. This operator is in front, so the operation is done first and then the value is assigned;

(--b) 4 is passed down by (b--) The value is 5. The operator operates first and then assigns the value, so the final value is 4;

(2) Assignment operator "=" is a well-understood operator;

var a=251; assign a value to the statement;

relational operator;

(greater than), == (equal to), < (less than), >= (greater than or equal to), <= (less than or equal to),! = (not equal), === (true equal, congruent), =....

The operation result of the relational operator is Boolean type: true (true), false (false);

, < ;, ==, >=, =< are all used to compare numerical types. The comparison methods and algorithms are the same as numeric strings; each relational operator will return a Boolean value;
  var a="3";
   var b="5";
   var c=a<b;
   console.log(c);//true
   var a=5;
   var b=8;
   var c=a<b;
   console.log(c);//true

letter type characters Strings can also be compared using greater than less than; the code point value of

  var a="alpha";
     console.log(a,typeof a);
     var b="blue";
     console.log(b,typeof b);
        var c=a<b;
     console.log(c,typeof c);//true

"a" is 97
"b"'s code point value is 98

"b"'s code point value is 98
Arrange in ascending order according to ASCII character order;

== can be used to compare numerical and numeric strings; For example;

   var b='5';
   var c=a==b;
   console.log(c);//true

=== is more advanced than ==. It is true and the two types cannot be compared. For example;

 var a=5;
   var b='5';
   var c=a===b;
   console.log(c);//false

(3) Logical operator; the output result is Boolean (boolean) type;

Logical operators are divided into three categories; logical OR | |; logical AND &&; logical NOT! ;

1. Logical OR ||: When one of the expressions on both sides evaluates to true (true), the result is true. If both sides are false, it must still be false.

Example;

      var a=3,b=4;
      var c=a>b||a<b;
      console.log(c);//true 有一侧结果为真 真个运算结果为真。
      var a=5,b=8;
      var c=a>b||a<4;
      console.log(c);//false  两边运算结果均为假 则输出结果为假 
 var a=5,b=8;
      var c=a<b||a>4;
      console.log(c);//true 两边运算结果均为真 则输出结果为真

2. Logical AND &&: The result of the operation is true only when the results on both sides of the operator are true,

Example;

      var a=5,b=8;
      var c=a<b&&a>4;
      console.log(c);//true  两边运算结果均为真 输出结果才为真
      var a=5,b=8;
      var c=a>b&&a>4;
      console.log(c);//false  两边运算结果有一侧为假 则输出结果为假
      var a=5,b=8;
      var c=a<b&&a>4;
      console.log(c);//false  两边运算结果有一侧为假 则输出结果为假

3. Logical negation! ;Get the opposite output result;

Example; This is the same as the example above. Add another! Becomes logical negation and takes the opposite value

      var a=5,b=8;
      var c=!a<b&&a>4;
      console.log(c);//true

The above is the detailed content of What are the js operators? Detailed introduction to js operators. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete