A detailed explanation of JavaScript expressions and operator _javascript tips

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

The JavaScript scripting language describes a set of operators for manipulating data values, including unary operators, Boolean operators, arithmetic operators, relational operators, ternary operators, bitwise operators, and assignment operators.
An expression is a "phrase" of a JavaScript language that contains variable names (or literal quantities) and operators. The simplest expression is the literal or variable name. Of course there are simple expressions that combine to create complex expressions.
one or one-dollar operator
(1) increment + + and decrement--

var box1=100; 
++box1;//is equivalent to box=box+1 
document.write ("box1=" +box1+ "<br/>");//Output box1=101 
var box2=100; 
--box2;//is equivalent to box=box2-1 

The difference between front and rear

var box=100; 
var age=++box;//box add 1 to 101 first, then assign the value to the age of the 
Height=box++;//box first assign to the height of the 101,box and then add to 102 
document.write ("age=" +age+ "<br/>");/output age=101 
document.write ("height=" +height+ "<br/>");//Output height=101 
document.write ("box=" +box);/output box=102, because the box has been accumulated two times, so it is 102 

In the absence of assignment operations, the front and rear are the same. However, in the assignment operation, if the increment or decrement operator is preceded, the predecessor operator accumulates or decrements and then assigns the value, and if it is the post operator, it is first assigned to add or subtract.
(2) addition and subtraction operators
used to take positive or negative operations, as well as to convert numeric strings to numeric forms.

var box = "; 
" document.write (typeof box+ "<br/>"); Output string 
var Age=-box; 
document.write (age+ "<br/>");//Output-20 

Ii. arithmetic operators
The JavaScript language provides five arithmetic operators, namely +,-,*,/and% (remainder). If the value of the arithmetic operator is not numeric, it is converted to a numeric value (implicit conversion) using the number () transformation function.

var box=100+ "; 
" document.write ("box=" +box+ "<br/>");//Output 100100 

What is this for? When you do arithmetic in a JavaScript language, as long as one of them is a string, the result is converted to a string. Equivalent to a string connector, which can no longer be counted as an addition arithmetic operator.

var box= " -10"; 
document.write ("box=" +box+ "<br/>");//output of 
var Age=5/4; 
document.write ("age=" +age+ "<br/>");//Output 1.25 
var height= ("Your Age is:" + (10+10));/Bracket Mandatory Priority 
document.write (height);//Output your age is: 20 

Take more

var box=10%3; 
document.write ("box=" +box);//Output 1 

Third, relational operators
The operators used for the comparison are said to be:< (less than),> (greater than), <= (less than equal), >= (greater than equal), = = (relative),!= (unequal), = = = (identical or congruent),!== (not congruent or not identical) as the relational operator. Most of the relational operators return a Boolean value.
As with other operators, the following rules are followed when a relational operator operates a non-numeric value:
12 operators are numeric, the numeric comparison
22 operands are strings, the character encoding values corresponding to two strings are compared
One of the 32 operands is a numeric value, the other is converted to a numeric value, which is compared
42 operands have one is an object, the value () method or the ToString () method is called first, then the result is compared.

var box1=3>2; 
document.write (box1+ "<br/>");/output True 
var box2= "3" >22; 
document.write (box2+ "<br/>");//Output False 
var box3= "3" > "; 
" document.write (box3+ "<br/>");/output True 
var box4= "a" > "B"//a for 97,b 
(document.write) box4+ /> ");/output True 
var box5=" Blue "<" alpha ";//blue's first letter is B,alpha's first letter is a,a for 97,b document.write 
(box5 )//Output True 

       on equal and unequal comparisons, if the operands are non-numeric, follow these rules:
       11 operands are Boolean, the comparison converts them to numeric values, and false turns to 0,true to 1.
       21 operands are strings, then compare them to numeric values before comparing them.
       31 operands are objects, call the value () method or the ToString () method before comparing.
       4 If no conversion is required, null and undefined are equal
        51 operands are nan, then = = Return false,!= returns True, and Nan and itself are unequal
       62 operands are objects. To compare whether they are the same object, and if they all point to the same object, return True, or false
       7 is equal in equality and all unequal judgments, such as values and types. Returns TRUE, otherwise returns FASLE.

 var box1= ' 2 ' ==2; 
document.write (box1+ "<br/>");//output True, compare only numeric Var box2={}=={}; 
document.write (box2+ "<br/>");//output False, because the comparison is their address, each newly created object has a different reference address. 
var box3=null==undefined; 
document.write (box3+ "<br/>");//output True because it is empty numeric var box4= ' 2 ' ===2; 
document.write (box4+ "<br/>");//Output False, the data type of two operands is not equal to Var box5=null===undefined; document.write (BOX5)//Output False, two operands have unequal data types 

Four logical operators
       the logical operators in the JavaScript language usually act on Boolean values, and are used in conjunction with relational operators, with three logical operators:& & (Logic and), | | (logical OR) and! (Logically not).
       (1) && indicates that both sides must be true to return true.
       If the operands on both sides have one operand that is not a Boolean value, and the operation does not necessarily return a Boolean value, the following rule follows:
       1 The first operand is an object, the second operand is returned
       2 The second operand is an object, The first operand returns true to return the second operand, or false
       31 operands are NULL, return null
        41 operands are undefined, then return undefined
       5 If one op is an object, the other is Boolean value that returns the
      logic and operator is a short-circuit operation, and if the first operand returns false, the second, whether true or false, returns false.

var box1={}&& (5>4); 
document.write (box1+ "<br/>");/output True 
var box2= (5>4) &&{}; 
document.write (box2+ "<br/>");//Output [object] 
var box3= (3>4) &&{}; 
document.write (BOX3);//Output False 

(2) | | Indicates that one of the sides is true and returns True.
If the operands on both sides have one operand that is not a Boolean value, and the operation does not necessarily return a Boolean value, the following rules are followed:
1 The first operand is an object, then the first operand is returned
2 The first operand evaluates to Fasle, the second operand is returned
32 operands are objects, the first operand is returned
42 operands are NULL, NULL is returned
52 operands are undefined, then return undefined
62 operands are Nan, then return nan
A logical OR operator is also a short-circuit operation, and if the first operand returns true, the second, whether true or false, returns TRUE.

var box1={}| | (5>4); 
document.write (box1+ "<br/>");//Output [object] 
var box2= (5>4) | | {}; 
document.write (box2+ "<br/>");/output True 
var box3= (3>4) | | {}; 
document.write (BOX3);//Output [object] 

(3)! A logical non-operator can function with any value, regardless of what data type the value is, this operator returns a Boolean value, the process is: first converts this value to a Boolean value, and then reverse, the rules are as follows:
1 operand is an object that returns false
The 2 operand is an empty string that returns True
3 operand is a non-empty string that returns false
4 operand is number 0, returns True
5 operand is any non 0 value, returns false
6 operand is null, returns True
7 operand is Nan, returns true
8 operand is undefined, returns true

var box=! {}; 
document.write (box);//Output False 

Five, bitwise operators
The JavaScript language includes seven bitwise operators: ~ (bit non),& (bit and), | (bit or), ^ (bitwise XOR),<< (left),>> (with Fu Yi shift),>>> (unsigned Right shift)
(1) A bit-non (~) operation converts the arithmetic into a 32-digit number, and then converts the binary number into its binary inverse code, and finally converts the binary number to a float. In essence, the number is negative, then minus 1 is the resulting value.

var box=~25; 
document.write (box);//Output-26 

(2) Bit and (&) operations directly to the binary form of the number of operations, and then the upper and lower two digits in the same position and operation, only two digits are 1 o'clock to reach 1, the rest are 0.

var box=25&3; 
document.write (box);//Output 1 

(3) bit or (|) The operation is also directly to the binary form of the number of calculations, and then the same position at the top of the two digits or operations, only the right two digits are 0 o'clock to reach 0, the rest are 1.

var box=25|3; 

(4) Bitwise XOR or (^) is also directly to the binary form of the operation. When only one digit is stored at 1 o'clock, it returns 1. The remainder returns 0. That is, two digits of the same time return 0, not at the same time return 1.

var box=25^3; 

(5) The left shift operation also operates on a binary number equal to the product of the first operand multiplied by the left shift power of 2.

var box=25<<3; 

(6) A signed right shift operation is also a binary number, which is equal to the quotient of the first operand divided by the right shift power of 2.

var box=24>>2; 
document.write (box);//Output 6 

(7) The unsigned Right shift operation is also the binary number of operations, for positive numbers, with the symbol right move is the same result, but for negative numbers, it will be different.

Vi. Assignment operators
The assignment operators include : = (), + = (), = = (), *= (),/= (),%= (), <<= (), >>= (), >>>= ().

var box=100; 
box+=100;//is equivalent to box=box+100 
document.write ("box=" +box);//Output box=200 

Vii. Other operators
1, string operator : "+", which is to add two strings. Rule: just have a string.

var box=100+ "10"; 

2, comma operator , you can perform multiple actions in a single statement

var box=100,age=200,height=300; 

3), ternary operator:

var box= (3>4)? " To ": Wrong"; 
document.write (box);//Output error 

For a more detailed understanding of the knowledge of the ECMAScript operator, you can access a detailed operator tutorial in this series of the Ecmasscript unary operators in the JavaScript Advanced tutorial. For JS operators, we can compare c++,c# and Java to learn, this is quite easy.

The above is all about the expressions and operators of JavaScript, and I hope it helps us to learn.

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.