The JavaScript scripting language describes a set of operators for manipulating data values, including unary operators, Boolean operators, arithmetic operators, relational operations
operator, ternary, bitwise, and assignment operators.
An expression is a "phrase" in the JavaScript language that contains variable names (or literals) and operators. The simplest expression is the literal or variable name. When
There are also simple expressions that combine to create complex expressions.
One by one-tuple operator
(1) increment + + and decrement--
var box1=100; ++box1;//equivalent to box=box+1 document.write ("box1=" +box1+ "<br/>");//Output box1=101 var box2=100; --box2;//equivalent to box=box2-1 document.write ("box2=" +box2);//Output box2=99
The difference between front and rear
var box=100; var Age=++box;//box first add 1 to 101, and then assign to the age of 101 var Height=box++;//box first assigns a value to the height of the 101,box and then accumulates as 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 box has been accumulated two times, so it is 102
In the absence of an assignment operation, the front and back are the same. However, if the increment or decrement operator precedes the assignment operation, the predecessor operator first
Add or subtract re-assignment, or, if the post-operator, the first value is added or subtracted.
(2) plus and minus operators
Used to take a positive or negative operation, but also to convert a numeric string into a numeric form of the function.
var box = "a"; document.write (typeof box+ "<br/>");//output string var Age=-box; document.write (age+ "<br/>");//Output -20 document.write (typeof age);//Output number
Two arithmetic operators
The JavaScript language specifies five arithmetic operators, +,-,*,/and% (take-away). If the value of the arithmetic operator is not a value, then it
First, use the number () transformation function to convert it to a numeric value (implicit conversion).
var box=100+ "+"; document.write ("box=" +box+ "<br/>");//Output 100100 document.write (typeof box);//Output string
What is this for? When you do arithmetic in the JavaScript language, as long as one of them is a string, the result is converted to a string. Quite
To a string connector, which can no longer be counted as an addition arithmetic operator.
var box= " -10"; document.write ("box=" +box+ "<br/>");//output is var Age=5/4; document.write ("age=" +age+ "<br/>");//Output 1.25 var height= ("Your Age is:" + (10+10));//parentheses Enforce precedence document.write (height);//Output your age is: 20
Take surplus
var box=10%3; document.write ("box=" +box);//Output 1
Three relational operators
The operator for comparison is called as the relational operator:< (less than),> (greater than), <= (less than equals), >= (greater than or equal), = = (relative), = = (not
etc.), = = = (identical or congruent),!== (not congruent or not identical). Most of the relational operators return a Boolean value.
As with other operators, the following rules are followed when the relational operators manipulate non-numeric values:
12 operators are numeric, the numeric comparison
22 operands are strings, compare the character encoded values of the two strings
32 operands have one is a numeric value, the other is converted to a numeric value, and a numeric comparison
42 operands have one object, the value () method or the ToString () method are called before 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 97,b as the document.write (box4+ "<br /> ");//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 an equal and unequal comparison, if the operand is a non-numeric value, the following rule is followed:
11 operands are Boolean, then the comparison is converted to a numeric value, and false turns into 0,true to 1.
21 operands are strings, then compare them before comparing them to a number.
31 operands are objects, the value () method or the ToString () method are called before the comparison.
4 null and undefined are equal if no conversion is required
51 operands are nan, = = Returns false,!= returns True, and Nan and itself are not equal
62 operands are objects, if they are the same object, if they point to the same object, return true, otherwise false
7 returns True if the value and the type are equal in the judgment of congruent and all-unequal, otherwise returns FASLE.
var box1= ' 2 ' ==2; document.write (box1+ "<br/>");//output True, compare only the value var box2={}=={}; document.write (box2+ "<br/>");//output false because their addresses are compared, and each newly created object has a different reference address. var box3=null==undefined; document.write (box3+ "<br/>");//output True because both are empty values var box4= ' 2 ' ===2; document.write (box4+ "<br/>");//Output False, the data type of two operands is unequal var box5=null===undefined; document.write (BOX5);//output false, data type of two operands is unequal
Four logical operators
The logical operators in the JavaScript language typically act on Boolean operations, typically in conjunction with relational operators, with three logical operators:&&
(Logic and), | | (logical OR) and! (logical non).
(2) && indicates that both sides must be true before returning true.
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 an object, then the second operand is returned
2 The second operand is an object, the first operand returns true before the second operand is returned, otherwise false
31 operand is NULL, return null
41 operands are undefined, then return undefined
5 If one operand is an object and the other is a Boolean value, the object is returned
The logic and operator are short-circuiting operations, and if the first operand returns false, the second returns false, whether true or false.
var box1={}&& (5>4); document.write (box1+ "<br/>");//Output True var box2= (5>4) &&{}; document.write (box2+ "<br/>");//Output [Object Object] var box3= (3>4) &&{}; document.write (BOX3);//Output False
(2) | | Indicates that one of the two sides is true and returns True.
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 an object, the first operand is returned
2 The evaluation result of the first operand is fasle, then the second operand is returned
32 operands are objects, then the first operand is returned
42 operands are NULL, returns null
52 operands are undefined, return undefined
62 operands are Nan, a nan is returned
The logical OR operator is also a short-circuiting operation, and if the first operand returns true, the second returns true whether it is true or false.
var box1={}| | (5>4); document.write (box1+ "<br/>");//Output [Object Object] var box2= (5>4) | | {}; document.write (box2+ "<br/>");//Output True var box3= (3>4) | | {}; document.write (BOX3);//Output [Object Object]
(3)! A logical non-operator can function with any value, regardless of what data type the value is, and the operator returns a Boolean value whose flow
is: first convert the value to a Boolean, then reverse, the rule is as follows:
1 operand is an object that returns false
2 operand is an empty string that returns True
3 operand is a non-empty string that returns false
4 operand is a value of 0, returns True
5 operand is any non-0 value, returns false
6 operand is null, returns True
7 operand is Nan, return true
8 operand is undefined, returns true
var box=! {}; document.write (box);//Output False
Five-bit operators
The JavaScript language includes seven bitwise operators: ~ (bitwise NON),& (bits and), | (bit or), ^ (bitwise XOR),<< (shift left),>> (with aligns shift number),
>>> (unsigned Right shift)
(1) The bitwise NON (~) operation converts the operand to a 32-digit number, then converts the binary number to its binary counter code, and finally converts the binary number to a floating-point
Number. In essence, the number is negative, and then minus 1 is the resulting value.
var box=~25; document.write (box);//Output-26
(2) Bit and (&) operation 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
All only 1 was reached at 1 o'clock, and the rest were 0.
var box=25&3; document.write (box);//Output 1
(3) bit or (|) The operation is also calculated directly on the binary form of the number, and then the two digits in the upper and lower positions are performed or computed, with only two digits right
All the bits are 0 o'clock and 0, the rest are 1.
var box=25|3; document.write (box);//Output 27
(4) Bit XOR (^) is also the direct operation of the binary form. When only one digit is stored 1 o'clock, it returns 1. The rest returns 0. That is
Two digits return 0, not 1 at the same time.
var box=25^3; document.write (box);//Output 26
(5) The left-shift operation is also a binary number, equal to the product of the first operand multiplied by (2 of the left shift number power).
var box=25<<3; document.write (box);//25 left 3 bits equals 25 times (2 of 3 powers), so output 200
(6) A signed right-shift operation is also a binary number that is equal to the quotient of the first operand divided by the power of the right Shift number (2).
var box=24>>2; document.write (box);//Output 6
(7) Unsigned Right shift operation is also the binary number of operations, for positive numbers, with a signed right shift is the same result, but for negative numbers, there will be
Different.
For a detailed understanding of the ECMAScript bitwise operators, you can access the ECMAScript bit operator.
Six assignment operators
The assignment operators include: = (), + = (),-= (), *= (),/= (),%= (), <<= (), >>= (), >>>= ().
var box=100; box+=100;//equivalent to box=box+100 document.write ("box=" +box);//Output box=200
Seven other operators
1 string Operator: "+", which is the function of two strings to be added. Rule: As long as you have a string.
var box=100+ "ten"; document.write ("box=" +box);//Output 100100
2 comma operator, you can perform multiple operations in one statement
var box=100,age=200,height=300; document.write ("box=" +box);//Output box=100
33-dollar Operator:
var box= (3>4)? " Right ":" Wrong "; document.write (box);//Output error
If you want to learn more about the knowledge of the ECMAScript operator, you can access the Ecmasscript unary operator in the advanced JavaScript tutorial. This
A detailed operator tutorial is available in the series. For JS operators, we can compare c++,c# and Java to learn, this is quite easy
Of
Easy learning JavaScript Six: Expressions and operators for JavaScript