Explanation of JavaScript expressions and operator _ javascript skills

Source: Internet
Author: User
This article mainly introduces JavaScript expressions and operators. If you need them, you can refer to the JavaScript script language to describe a group of operators used to operate data values, including unary operators and boolean operators, arithmetic Operators, Relational operators, ternary operators, bitwise operators, and value assignment operators.
An expression is a "phrase" in JavaScript language that contains the variable name (or literal) and operator. The simplest expression is the literal or variable name. Of course, you can also combine simple expressions to create complex expressions.
I. unary Operators
(1) Increment ++ and decrease --

Var box1 = 100; ++ box1; // equivalent to box = box + 1 document. write ("box1 =" + box1 +"
"); // Output box1 = 101 var box2 = 100; -- box2; // equivalent to box = box2-1 document. write ("box2 =" + box2); // output box2 = 99

Difference between front and back

Var box = 100; var age = ++ box; // box first accumulates 1 to 101, and then assigns the age to 101 var height = box ++; // box first assigns height to 101, and box accumulates to 102 document. write ("age =" + age +"
"); // Output age = 101 document. write (" height = "+ height +"
"); // Output height = 101 document. write (" box = "+ box); // output box = 102, because the box has been accumulated twice, so it is 102

In the absence of a value assignment operation, the front and back are the same. However, in the Value assignment operation, if the increment or decrement operator is prefixed, the prefix operator will first accumulate, subtract, and then assign values. If the operator is a postfix operator, assign values first and then accumulate or subtract.
(2) addition and subtraction Operators
It is used for positive or negative operations, and can also be used to convert numeric strings into numeric forms.

Var box = "20"; document. write (typeof box +"
"); // Output string var age =-box; document. write (age +"
"); // Output-20 document. write (typeof age); // output number

Ii. Arithmetic Operators
The JavaScript language specifies five Arithmetic Operators, namely +,-, *,/, and % (remainder ). If the value of the arithmetic operator is not a value, it will first use the Number () transformation function to convert it to a value (implicit conversion ).

Var box = 100 + "100"; document. write ("box =" + box +"
"); // Output 100100 document. write (typeof box); // output string

Why? When performing arithmetic operations in JavaScript, if one of them is a string, the result is converted to a string. It is equivalent to a string connector and cannot be counted as an addition arithmetic operator.

Var box = "100"-10; document. write ("box =" + box +"
"); // Output 90 var age = 5/4; document. write (" age = "+ age +"
"); // Output 1.25 var height = (" your age is: "+ (10 + 10); // brackets force priority document. write (height); // output your age: 20

Remainder

Var box = 10% 3; document. write ("box =" + box); // output 1

Iii. Relational operators
The operators used for comparison are called Relational operators: <(less than),> (greater than), <= (less than or equal to), >=( greater than or equal ), = (relative ),! = (Unequal), === (constant or full ),! = (Incomplete or not constant ). Most Relational operators return a Boolean value.
Like other operators, Relational operators must follow the following rules when operating non-numeric values:
1. If both operators are numerical values, the values are compared.
2. If both operands are strings, the character encoding values corresponding to the two strings are compared.
3. If one of the two operands is a numerical value, the other is converted to a numerical value.
4. If either of the two operands is an object, call the value () method or toString () method first and then compare the result.

Var box1 = 3> 2; document. write (box1 +"
"); // Output true var box2 =" 3 "> 22; document. write (box2 +"
"); // Output false var box3 =" 3 ">" 22 "; document. write (box3 +"
"); // Output true var box4 =" a ">" B "; // a is 97, B is 66 document. write (box4 +"
"); // Output true var box5 =" Blue "<" alpha "; // The first letter of Blue is B, the first letter of alpha is a, and a is 97, B is 66 document. write (box5) // outputs true

In comparison of equal and unequal values, if the operands are non-numeric values, the following rules are followed:
1. If an operand is a Boolean value, convert it to a value before comparison, convert false to 0, and convert true to 1.
2. If an operand is a string, convert it to a value before comparison.
3. If an operand is an object, call the value () method or toString () method before comparison.
4. If no conversion is required, null and undefined are equal.
If an operand of 5 is NaN, = returns false ,! = True is returned, and NaN is different from itself.
6. If both operands are objects, check whether they are the same object. If they all point to the same object, true is returned; otherwise, false is returned.
7. true is returned only when the values and types are equal in the case of full equality and full inequality. Otherwise, fasle is returned.

Var box1 = '2' = 2; document. write (box1 +"
"); // Output true, compare the value var box2 ={}={}; document. write (box2 +"
"); // Output false because their addresses are compared, and the reference addresses of each newly created object are different. Var box3 = null = undefined; document. write (box3 +"
"); // Output true, because both are null values var box4 = '2' = 2; document. write (box4 +"
"); // Output false. The data types of the two operands are not equal. var box5 = null === undefined; document. write (box5); // output false. The data types of the two operands are not equal.

Four logical operators
Logical operators in JavaScript usually act on Boolean operations. They are generally used together with Relational operators. There are three logical operators: & (logical and), | (logical or) and! (Not logically ).
(1) & indicates that both sides must be true to return true.
If one of the operands on both sides is not a Boolean value, a Boolean value is not necessarily returned for the operation. The following rule is followed:
1. If the first operand is an object, the second operand is returned.
2. If the second operand is an object, true is returned for the first operand. Otherwise, false is returned.
3. If an operand is null, null is returned.
4. If an operand is undefined, undefined is returned.
5. If one operation is an object and the other is a Boolean value, this object is returned.
The logic and operator are short-circuit operations. If the first operand returns false and the second operand returns false, both true and false.

Var box1 ={}& & (5> 4); document. write (box1 +"
"); // Output true var box2 = (5> 4) & amp; {}; document. write (box2 +"
"); // Output [object Object] var box3 = (3> 4) & {}; document. write (box3); // output false

(2) | returns true if either of them is true.
If one of the operands on both sides is not a Boolean value, a Boolean value is not necessarily returned for the operation. The following rule is followed:
1. If the first operand is an object, the first operand is returned.
2 If the evaluate result of the first operand is fasle, the second operand is returned.
3. If both operands are objects, the first operand is returned.
4 if both operands are null, null is returned.
If both operands are undefined, the return value is undefined.
If both operands are NaN, NaN is returned.
The logic or operator is also a short-circuit operation. If the first operand returns true, the second returns true whether it is true or false.

Var box1 ={}| | (5> 4); document. write (box1 +"
"); // Output [object Object] var box2 = (5> 4) |{}; document. write (box2 +"
"); // Output true var box3 = (3> 4) | |{}; document. write (box3); // output [object Object]

(3 )! Logical non-operators can work with any value. No matter what data type the value is, this operator returns a Boolean value. The process is to convert the value to a Boolean value first, then, the rule is as follows:
1. the operand is an object and false is returned.
2. returns true if the operand is an empty string.
3. If the operand is a non-null string, false is returned.
4. If the operand is 0, true is returned.
5. If the operand is any non-0 value, false is returned.
6. If the operand is null, true is returned.
7. If the operand is NaN, true is returned.
8. If the operand is undefined, true is returned.

Var box =! {}; Document. write (box); // output false

5. bitwise operators
JavaScript includes seven bitwise operators :~ (Not), & (bit and), | (bit or), ^ (bit or), <(left shift),> (shift to the right with a letter), >>> (shift to the right without a symbol)
(1) Non (~) The operation converts the operation number to a 32-bit number, converts the binary number to its binary inverse code, and finally converts the binary number to a floating point number. In essence, it is to calculate the negative value for the number, and then minus 1 is the obtained value.

Var box = ~ 25; document. write (box); // output-26

(2) bitwise AND (&) operations directly perform operations on the binary form of the number, and then perform operations on the two digits at the same position up and down. Only when the two digits are 1 can we get 1, the remaining values are 0.

Var box = 25 & 3; document. write (box); // output 1

(3) bitwise OR (|) operations are also directly performed on the binary form of the number, and then the two digits in the upper and lower positions are operated or, 0 is obtained only when both digits on the right are 0, and the rest are 1.

Var box = 25 | 3; document. write (box); // output 27

(4) bitwise XOR (^) is also a direct binary operation. If only one digit stores 1, it returns 1. The rest return 0. That is, two digits return 0 at the same time and 1 at the same time.

Var box = 25 ^ 3; document. write (box); // output 26

(5) The Left shift operation is also an operation on the binary number, which is equal to the product where the first operand is multiplied by (the power of the number of left shifted digits of 2.

Var box = 25 <3; document. write (box); // 25 shifts three digits to the left, which is equivalent to 25 multiplied by (3 power of 2), so the output is 200

(6) The signed right shift operation is also an operator that operates on binary numbers, equal to dividing the first operand by (the right shift digit power of 2.

Var box = 24> 2; document. write (box); // output 6

(7) The unsigned right shift operation also operates on binary numbers. For positive numbers, it is the same as the signed right shift operation, but for negative numbers, it is different.

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

Var box = 100; box + = 100; // equivalent to box = box + 100 document. write ("box =" + box); // output box = 200

7. Other operators
1) string Operators: "+", Which is used to add two strings. Rule: Only one string is needed.

Var box = 100 + "10" '; document. write ("box =" + box); // output 100100

2) comma OperatorYou can execute multiple operations in one statement.

Var box = 100, age = 200, height = 300; document. write ("box =" + box); // output box = 100

3). Ternary operators:

Var box = (3> 4 )? "Right": "error"; document. write (box); // Output Error

If you want to learn more about ECMAScript operators, you can visit the ECMASscript unary operator series in the JavaScript advanced tutorial. For JavaScript operators, we can compare C ++, C #, and Java. This is quite easy.

The above is all about JavaScript expressions and operators. I hope it will be helpful for your learning.

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.