3. Operators

Source: Internet
Author: User
Tags comparison table

Operator

Learning Essentials:
1. What is an expression
2. Unary operators
3. Arithmetic operators
4. Relational operators
5. Logical operators
6.* Bitwise operators
7. Assignment operators
8. Other operators
9. Operator Precedence

ECMAScript-262 describes a set of operators for manipulating data values, including unary operators, Boolean operators, arithmetic
Operators, relational operators, ternary operators, bitwise operators are assignment operators. The operators in ECMAScript apply to
Many values, including string, numeric, Boolean, object, and so on. When applied to an object, the valueof () of the object is usually called and
ToString () method to obtain the corresponding value.


PS: The typeof operator, the new operator, can also be called the TypeOf operator, the new operator, is the same
a meaning.

I. What is an expression

An expression is a "phrase" in ECMAScript, and the interpreter converts it to a value by calculation. The simplest expression
Type is the literal or variable name. For example:
5.96//Numeric literal
' Journey '//string literal
true//boolean literal
null//Empty value literal
/java///Regular Expression literals
{x:1,y:2}//object literal, object expression
[1,2,3]//array literal, array expression
function (n) {return x+y;} function literal, function expression
box//variable


Of course, you can also create complex expressions by merging simple expressions. Like what:
box+5.96//An expression of an addition operation
typeof (Box)//view expressions for data types
box>8//Logical Operation expressions


From the above narrative, we know that a single literal and a combination of literal operators can be called expressions.

Two or one-tuple operator

An operator that can manipulate only one value is called a unary operator.

1. Increment + + and decrement--
var box = 100;//
++box;//the box with a 1, equivalent to box=box+1
--box;//box to a 1, equivalent to Box=box-1
box++;//Ibid.
box--;//Ibid.

2. The difference between pre-and post-position
In the absence of an assignment operation, the front and back are the same. However, in the assignment operation, if the increment or decrement operator is pre-
The preceding operators accumulate or subtract and then assign values, and if they are the post operators, they are first assigned and then accumulated or subtracted.

var box = 100;
var age = ++box;//age value is 101 box is first added to 101 and then assigned to age
var height = box++;//height value is first assigned to height, then plus 1

3. Other types of rules that apply unary operators
var box = ' box++//90 '; value string automatically converted to numeric value
var box = ' ab '; Box++//nan, string contains non-numeric converted to NaN
var box =false;box++//1,false turns into a value of 0, and the summation is 1.
var box = 2.3;box++//3.3, direct plus 1
var box = {
Tostring:function () {
return 1;
}
box++;
};//1, do not set ToString or valueof is Nan

4. Add and Subtract operators
var box = 100;+box//100, no effect on the value
var box = ' +box//89 '; value string converted to numeric value
var box = ' ab '; +box//nan, string contains non-numeric converted to NaN
var box = false;+box//0, Boolean converted to corresponding value
var box = 2.3;+box//2.3, no change
var box = {
Tostring:function () {
return 1;
}
+box;
}//1, do not set ToString or valueof is Nan


var box = 100;-box//-100, no effect on the value
var box = '-box//-89 '; value string converted to numeric value
var box = ' ab ';-box//nan, string contains non-numeric converted to NaN
var box = false;-box//0, Boolean converted to corresponding value
var box = 2.3;-box//-2.3, no change
var box = {
Tostring:function () {
return 1;
}
-box;
}//-1, do not set ToString or valueof is Nan

Note: addition and subtraction are generally used for arithmetic operations, and type conversions can be made to the above.


Three, arithmetic operators

ECMAScript defines 5 arithmetic operators, subtraction for modulo (take-up). If the value in the arithmetic operation is not a number,
Then the background will first use the number () transformation function to convert it to a numeric value (implicit conversion).

1. Addition

var box = 1+ 2;//equals 3
var box = 1+ Nan;//nan, as long as there is a Nan is Nan
var box = Infinity + infinity;//infinity
var box =- infinity+-infinity;//-infinity
var box = infinity+-infinity;//nan, positive infinity and negative infinity add equals NaN
var box = 100+ ' 100 ';// 100100, String link, string is not an addition, as long as its
has a string, then automatically use the String connector
var box = ' Your age is: ' +10+20;//your age is: 1020, is converted to a string, before the
Var box = 10+20+ ' is your age ';//30 is your age and is not converted to a string, before the
var box = ' Your age is: ' + (10+20);//Your Age is: 30, not converted to a string
var box = + + Object//10 [Object Object], if no ToString () or valueof ()
Returns the value of the number of top-up returns


2. Subtract
var box = 100-70;//equals $
var box = -100-70;//equals -170
var box = -100--70;//-30, generally written -100-(-70) more clearly
var box = 1-nan;//nan, as long as there is a Nan is Nan
var box = Infinity-infinity;//nan
var box =-infinity--infinity;//nan
var box = infinity--infinity;//infinity
var box =-infinity-infinity;//-infinity
var box = 100-true;//99,ture turns into a value of 1 Br>var box = 100-';//100, ' turn to 0
var box = 100-' 70 ';//30, ' 70 ' to value
var box = 100-' null ';//100,null to 0
var box = 1 00-' journey ';//nan,journey to NaN
var box = 100-object;//nan, if either ToString () or valueof ()
Returns 10-the value of the returned number


3. Multiply
var box = 70;//7000
var box = Nan;//nan, as long as there is a Nan for nan
var box = Infinity * Infinity;//inf Inity
var box =-infinity * infinity://-infinity
var box =-infinity *-infinity;//infinity
var box = $ * ture; 100,true turns into a value of 1
var box = 100 * ';//0, ' change to 0
var box = * null;//0,null to 0
var box = * ' Journey '//nan,jour Ney to NaN
var box = 100 * object;//nan returns the value of 10* return number if there is ToString () or valueof ()

4. Division
var box = 100/70;//1.42 ...
var box = 100/nan;//nan
var box = Infinity/infinity;//nan
var box =-infinity/infinity://nan
var box =-infinity/-infinity;//nan
var box = 100/ture;//100,true turns into a value of 1
var box = +/';//infinity
var box = 100/null;//infinity
var box = +/' Journey '//nan
var box = 100/object;//nan, if there is ToString () or valueof ()
Returns the value of the 10/return number


5. Mould-Finding
var box = 10 3;//1 with a remainder of 1
var box = Nan;//nan
var box = Infinity% Infinity;//nan
var box =-infinity% Infinity://nan
var box =-infinity%-infinity;//nan
var box = ture;//0
var box = [+];//nan
var box = Null;//nan
var box = ' Journey '//nan
var box = 100 object;//nan, if there is ToString () or valueof ()
Returns the value of 10% return number


Four, relational operators

The operators used for comparison are called relational operators: less Than (<), greater than (>), less than or equal (<=),
Greater than or equal (>=), equality (= =), unequal (! =), congruent (= = =),
Non-congruent (not identical) (!==)

As with other operators, the following rules are followed when the relational operators manipulate non-numeric values:
1. Two operands are numeric, then numerical comparison;
2. Two operands are all strings, then the character encoding corresponding to two strings is compared;
3. The two operand has one value, then the other is converted to a numeric value, and then the numerical comparison is performed;
4. The two operands have one object, then the valueof () method or the ToString () method are called before the results are compared;

var box = 3 > 2;//true
var box = 3 > 11;//false
var box = ' 3 ' > 22;//false, if there is only one numeric string, it will be converted to
Values, in comparison
var box = ' 3 ' > ' n ';//true, first to value and then compare 3>2
var box = ' A ' > ' b ';//falsea=97,b=98;
var box = ' A ' > ' B ';//truea=97,b=66;
var box = 1 > object;//false, if either ToString () or valueof () returns 1> return value


Again equal and unequal comparisons, if the operands are non-numeric, follow these rules:
1. An operand is a Boolean value, then it is converted to a number before comparison, and false to 0,true to 1;
2. An operand is a string, then it is converted to a numeric value before comparison;
3. An operand is an object, then the valueof () or ToString () method is called before the comparison with the return value;
4. If no conversion is required, null and undefined are equal;
5. One operand is nan, then = = Returns false,! = Returns True, and Nan is not equal to itself;
6. Two operands are objects, then they are the same object, and if they all point to the same object, return True.
Otherwise, false is returned.
7. The comparison value and the type all want to wait for the equality and all-unequal judgment to return true, otherwise false is returned.

var box = 2 ==2;//true
var box = ' 2 ' ==2;//true
var box = False ==0;//true,false will be converted to a value of 0
var box = ' a ' = = ' a ';//false, the converted code is not the same
var box = 2 =={};//false, executing tostring () or valueof () will change
var box = 2 ==nan;//false, as long as NaN is false
var box = {}=={};//false, compared to their address, each newly created object has a different reference address
var age = {};
var height = age;
var box = age = = Height;//ture, same as reference address, so equal
var box = ' 2 ' ==2//false, both values and types must be equal
var box = 2!==2//false, values and types all want to wait.

Special Value Comparison table
Value of an expression
Null==undefinedtrue
' NaN ' ==nanfalse
5==nanfalse
Nan==nanfalse
False==0true
True==1true
True==2false
Undefined==0false
Null==0false
' ==100true '
' ===100false '

Five, logical operators

Logical operators are typically used for Boolean operations, generally with relational operators, with three logical operators;
(and), logical or (or), logical non (not).

1. Logic and (and):&&
var box = (5>4) && (4>3)//true, both sides are true, return true

First operand second operand third operand
Truetruetrue
Truefalsefalse
Falsetruefalse
Falsefalsefalse

If the operands on both sides have an operand that is not a Boolean value, and the operation does not necessarily return a Boolean value, the following rule is followed:

1. The first operand is the object, then the second operand is returned;
2. The second operand is the object, and the first operand returns true to return the second operand, otherwise false;
3. If one of the operands is NULL, returns null;
4. If one of the operands is undefined, the undefined is returned.

var box = Object && (5>4);//true, returns the second operand
var box = (5>4) && objects;//[object Object]
var box = (3>4) && object;//false
var box = (5>4) &&null;//null

The logic and operator are short-circuiting operations, as the name implies, if the first operand returns false, the second number returns false whether true or false

var box = true&&age;//error, age undefined
var box = False&&age;//false, do not perform age.

2. Logic or (OR): | |
var box = (9>7) | | (7>8);//true, as long as there is one side on both sides is true, returns True

First operand result of second operand
Truetruetrue
Truefalsetrue
Falsetruetrue
Falsefalsefalse

If the operands on both sides have an operand that is not a Boolean value, the logic and operation do not necessarily return a Boolean value, at which point the following rules are followed:


1. The first operand is the object, then the first operand is returned;
2. The first operand, which evaluates to False, returns the second operand;
3. Two operands are objects, the first operand is returned;
4. Two operands are null, then NULL is returned;
5. The two operands are Nan, then the Nan is returned;
6. Two operands are undefined, then return undefined;

var box = Object | | (5>3);//[object Object]
var box = (5>3) | | object;//true
var box = Object 1| | Objects 2;//[object Object]
var box = null| | Null;//null
var box = nan| | Nan;//nan
var box = undefined| | undefined;//undefined

Like logic and operators, a logic or operator is also a short-circuit operation. When the first operand evaluates to true, the second operand is not evaluated.

var box = true| | Age;//true
var box = false| | age;//error, age undefined


We can use the logical OR operator feature to avoid assigning null or undefined values to variables.


3. Logical non (NOT):!

A logical non-operator can be used with any value. Regardless of the data type of this value, this operator returns a Boolean value. The process is: first convert this value to a Boolean value, and then take the inverse, the rule is as follows:

1. The operand is an object that returns false;
2. The operand is an empty string and returns true;
3. The operand is a non-empty string and returns false;
4. The operand is a value of 0 and returns false;
5. The operand is any non-0 value (including infinity), false;
6. The operand is null and returns true;
7. The operand is Nan and returns true;
8. The operand is undefined and returns true;

var box =! (5>4);//false
var box =! {};//false
var box =! '; /true
var box =! ' Lee ';//false
var box =!0;//true
var box =!8;//false
var box =!null;//true
var box =! Nan;//true
var box =!undefined;//true

Using a logical non-operator, the process is to turn the value into a Boolean value and then reverse. The use of two logical non-operators is to turn the value into a Boolean to reverse the inverse, equivalent to the value of a Boolean () Transformation function processing.

var box =!! 0;//false
var box =!! Nan;//false

In general, a Boolean value can be obtained using a logical non-operator and two logical non-operators, while there is no error in using more than three logical non-operators, but it does not make sense.

Six, * bitwise operators

PS: In general applications, we basically use an operator that is not in place. Although, it is based on the underlying, performance and speed will be very good, but because the lower level, the use of a lot of difficulty.

There are seven bitwise operators, namely, bit non-not (~), Bit and and (&), bit or OR (|), bitwise XOR (^), left Shift (<<), signed right Shift (>>), unsigned Right shift (>>>).
var box = ~25;//-26;
25 equals 00000000000000000000000000011001.
Convert to 11111111111111111111111111100110
Output "-26"

var box = 25&3;//1
25 = 0000 0000 0000 0000 0000 0000 0001 1001
3 = 0000 0000 0000 0000 0000 0000 0000 0011
---------------------------------------------
and = 0000 0000 0000 0000 0000 0000 0000 0001

var box = 25|3;//27
var box = 25<<3;//200
var box = 25>>2;//6
var box = 25>>>2;//6

Seven, assignment operators

The assignment operator is denoted by the equals sign (=), which assigns the right value to the left variable.
var box = 100;//assigns 100 to the box variable

The compound assignment operator is represented by a x=, and X represents an arithmetic operator and a bitwise operator.
var box = 100;
box = Box + 100;//200, itself plus 100

This situation can be changed to write:
var box = 100;
Box + = 100;//200,+= instead of box+100

In addition to this +=j Plus/assign operator, there are several other:

1. Multiply/assign (*=)
2. Addition/Assignment (/=)
3, Die/Fu (%=)
4. Add/assign (+ =)
5. Subtract/assign (-=)
6. Shift Left/assign (<<=)
7. Signed Right Shift/Assignment (>>=)
8. Unsigned Right Shift/assignment (>>>=)


Viii. Other operators

1. String operators
The string operator has only one, that is: "+". Its purpose is to add two strings.
Rule: At least one operand is a string.

var box = ' 100 ' + ' 100 ';//100100
var box = ' 100 ' + 100;//100100
var box = 100 + 100;//200

2. Comma operator
The comma operator can perform multiple operations in a single statement.
var box = 100,age =, height = 178; Multiple variable declarations
var box = (1,2,3,4,5);//5, variable declaration, assigning the last value to a variable, not commonly used
var box = [1,2,3,4,5];//[1,2,3,4,5], the literal declaration of an array
var box = {//[object Object], object literal declaration
1:2,
3:4,
5:6
}

3. Ternary conditional operators

The ternary conditional operator is actually the shorthand form of the IF statement that will be learned later.
var box = 5>4? ' Right ': ' wrong ';
Yes, 5>4 returns true to assign ' pair ' to box, and vice versa.

Equivalent
var box = ';//Initialize variable
if (5>4) {//Judgment expression return value
box = ' yes ';//Assignment
}else{
box = ' wrong ';//Assignment
}

Nine, operator precedence

In general operations, we do not have to consider the precedence of operators, because we can solve this problem through parentheses, such as:
var box = 5-4 * 8;//-27
var box = (5-4) * 8;//8

However, if you do not use the parentheses lightweight priority, we must follow the following order:

Operator description
. [] () object member access, array subscript, function call, etc.
++ -- ~ ! Delete Mew typeof void unary operator
*/% multiplication, division, de-Mold
+-+ addition, subtraction, string connection
<<>> >>> Shift
<<=>>=instanceof relationship comparison, detection class instance
= = = = = = =!== Identity (congruent)
& Bit and
^ Bit XOR or
| bit or
&& Logic and
|| Logical OR
?: Ternary conditions
=x= Assignment, Operation assignment
, multiple assignments, array elements

3. Operators

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.