JavaScript operator Syntax Comprehensive Overview _ Basics

Source: Internet
Author: User
Tags arithmetic arithmetic operators bitwise

Front.

Most of the operators in JavaScript are represented by punctuation marks, and a few are represented by keywords, and their syntax is concise and their numbers are indeed quite large. Operators always follow some fixed syntax, and only if you understand and master them can you use the operators correctly. This article will focus on the syntax overview of JavaScript operators

Number of operations 

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

1 + 2;
true | | False

The unary operator in JavaScript (unary operator) converts an expression to a 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 the 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 precedence operator always executes before the operator with the lower precedence operator

A total of 46 operators are divided into 14-level precedence, from highest to lowest:

++ -- - + ~ ! Delete typeof void
 */%
 +-
 << >> >>>
 < <= > >= instanceof in
 = =!= = = =!==
 &
 ^
 |
&&
| |
?:
= *=/=%= + = = &= ^= |= <<= >>= >>>=
,

These 14-level operator precedence levels can be seen:

Unary operator > Arithmetic operator > comparison operator > Logical operator > Ternary operator > Assignment operator > Comma operator

[note] The logical fetch counter operator belongs to a unary operator with the highest precedence

Example

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

Like the above situation is more complex, step-by-step to decompose its order of operations

First compute a unary operator!,!2;//false

The expression then becomes
false < 1 && 4*3 + 1;

Calculate arithmetic Operators 4*3+1;//13

The expression then becomes
false < 1 && 13;

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

The expression then becomes:
true && 13;//13

You can use parentheses to force a specified order of operations

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

Combination of

Operators have two kinds of binding, one is from left to right, the mark is L, one is from right to left, and 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 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 determine their order of operations in complex expressions, but when the subexpression affects each other, the order is changed

Example

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

To parse the expression first, the increment operator, multiplication operator, addition operator, and assignment operator are calculated according to the order of precedence.

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

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

The result of the calculation a--;//is 2,a 1

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

Calculates the second a++;//result is 1,a 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 act on any data type, but still want their operands to be of the specified type, and most operators return a specific type of value, in the following operator rule table, the arrow is the type of the operator operand, and the arrow is the type of the result of the operation

"Left value"

The left value (Lvalue) is an ancient term that can only appear on the left side of the operator

In JavaScript, variables, object properties, and array elements are all left values

Increment operator + +, decrement operator--and the operand type of the assignment operator is the left value

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

Operator Rules table

Operator operation type + + increment lval->num--decrement lval->num--negation Num-&gt         num + Convert to digital num->num ~ Int->int!     Logical non-Bool->bool delete attribute Lval->bool typeof detection type any->str void return undefined Any->undef ****************************************************** * *% multiply, divide, find Yu Num,num->num *********** +-Add, subtract Num,num->num + string connection str,str->str * *      << left shift Int,int->int >> signed right Shift Int,int->int >>> unsigned Right shift int,int->int ****************************************************** & Lt <= > >= Compare numeric Order Num,num->bool < <= > >= compare alphabetical order str,str->bool instanceof test pair Like class Obj,func->bool in test properties Str,obj->boOL ****************************************************** = = Judge equal Any,any->bool!= judge unequal Any,a Ny->bool = = To judge the identity Any,any->bool!== to judge the non identity Any,any->bool ********************************         & Bitwise with Int,int->int ****************************************************** ^         Bitwise XOR or Int,int->int ****************************************************** | Bitwise OR Int,int->int ****************************************************** && logic and Any,any-&gt        ; Any ****************************************************** | | Logical OR Any,any->any ******************************************************?: conditional operator Bool,any,any->a     NY ****************************************************** = Assignment Lval,any->any *=/=%= + = = &= Operation and assignment Lval,any->any ^= |= <<= >>= >>>= ******************************, ignoring the first operand, Any,any->any returns the second operand 

The above JavaScript operator syntax Comprehensive overview is the small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.