JavaScript operator Syntax overview

Source: Internet
Author: User
Tags arithmetic arithmetic operators bitwise logical operators

Number of operations

There are 46 operators in JavaScript, and if they are categorized according to the number of their operands, most of them are two-dollar operators (binary operator) with two operands, which combine two expressions into complex expressions

1 + 2;true | | False

The unary operator (unary operator) in JavaScript converts an expression to another slightly more complex expression, consisting mainly of the following 9:

++ -- - + ~ ! Delete typeof void
A++;typeof true;

JavaScript has only one ternary operator (ternary operator), which is a conditional-judgment operator?: it merges three expressions into one expression

2>1? 2:1;

Priority level

Operator precedence controls the order in which operators are executed, and the execution of operators with high precedence is always preceded by operators with lower precedence operators

The 46 operators have a total of 14 priority levels, from high to Low:

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;float:left;border:none; "/>

1 + +---+ ~!  Delete typeof Void2 */%3 +-4 << >> >>>5 < <= > >= instanceof in6 = = = = = = = =!==7 &8 ^9 |10 &&11 | | : *=/=%= + = &= ^= |= <<= >>= >>>=14,

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;float:left;border:none; "/>

This 14-level operator priority level shows:

Unary operators > Arithmetic operators > Comparison operators > Logical operators > Ternary operators > Assignment operators > Comma operators

[note] The logical inverse operator is a unary operator with the highest precedence

Example

!2<1&&4*3+1;

Like the above, the situation is more complex, and gradually decompose its order of operations.

Computes the unary operator first!,!2;//false

Then the expression becomes false < 1 && 4*3 + 1;

Calculating arithmetic operators 4*3+1;//13

The expression becomes false < 1 && 13;

Calculation comparison operator <,false<1;//true

Then the expression becomes: true && 13;//13

You can use parentheses to force a specified order of operations

2+3*5;//17 (2+3) *5;//25;

Binding nature

Operators have two kinds of binding, one is from left to right, the notation is L, one is from right to left, the mark is R. binding specifies the order of operations in multiple operator expressions with the same precedence

Most operators have a left-to-right binding, and only the unary, conditional, and assignment operators have right-to-left binding

w = x + y + z;//equivalent to: w = ((x + y) + Z);
w = x = y = z;//equivalent to: w = (x = (y = z));
Q = a? B:c? D:e? f:g;//equivalent to: Q = a? B: (c. D: (E. f:g));

The precedence and binding of operators determines their order of operations in complex expressions, but the order changes when sub-expressions affect each other.

Example

A = 1;b = a++ + a--* a++;

In this expression, the increment operator, the multiplication operator, the addition operator, and the assignment operator are respectively evaluated according to the order of precedence.

Calculate first a++;//result to 1,a to 2

The expression becomes b = 1 + a--* a++;

Calculated a--;//results of 2,a to 1

The expression becomes b = 1 + 2 * a++;

Calculates a second a++;//result of 1,a of 2

The expression becomes b = 1 + 2 * 1;

So, finally a = 2; b = 3;

A = 1;b = a++ + a--* A++;console.log (A, b);//2 3
Similarly a = 1;b = a--* a++ + A++;console.log (A, b);//2,1

Type

Some operators can work on any data type, but still want their operands to be of the specified type of data, and most operators return a value of a specific type, in the following operator rule table, before the arrow is the type of the operator operand, and the type of the result of the operation after the arrow

"Left value"

An lvalue (Lvalue) is an ancient term that indicates that an expression can only appear to the left of the operator

In JavaScript, variables, object properties, and array elements are both lvalue

Increment operator + +, decrement operator--and the operand type of the assignment operator are Lvalue

var a = 3;a++;//33--;//error ({}). A + = ' 1 ';//' undefined1 ' test '-= ' test ';//Error

Operator Rule table

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;float:left;border:none; "/>

++                 Increment                   lval->--                  Volume Reduction                   lval->-                   Reverse                   num->+                   Convert to Digital              num->~                   bitwise negation                ->!                   Logic Non-                 bool->             Delete Properties                lval->              Detection Type                any->                Back to undefined         any->*************************  \ %               multiply, divide and            num,num->************** surplus *****************************+ -                Plus , minus                 num,num-> +                  String Connection              str,str->******************** <<                  left Shift                  ,->>>                  signed right Shift            ,->>> >                unsigned Right shift             ,->******************************************************<  <= > >=          Compare Numeric order             num,num->< <= > >=           Compare Alphabet Order          str,str->          Test Object Classes              obj,func->                  Test Properties               str, obj->******************************************************==                  judgment equals                any,any->!=                  Judging Unequal               any,any- >===                Judging identity                any,any->!==                 Judging non-identity              any,any->************************************************** &                   Bitwise AND                 ,- >******************************************************^                   Bitwise XOR               ,-> |                   bitwise OR                  ,->******************************************************& &                 Logic and                  any,any->******** **********************************************||                  logic or                  any,any->******** ?: &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&Nbsp;          Conditional Operators               bool,any,any->******************************************************=                    Assign Value                  lval, any->*= /= %=+= -= &=           Operation and Assignment              lval,any->^= |=  <<=>>= >>>=******************************************************->



JavaScript operator Syntax overview

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.