The various operators in JavaScript use the summary _ basics

Source: Internet
Author: User
Tags arithmetic numeric value

Unary operator
an operator that can manipulate only one value is called a unary operator.
The unary operator is the simplest operator in the ECMAScript.

1. Increment and decrement operators
the increment decrement operator borrows directly from C and has two versions: pre-and rear-type. Continue, the front should be positioned before the variable to be manipulated, and the rear type should be after the variable to be manipulated.
Front Type:

var num1 = 1;
var num2 = 2;
var num3 = ++num1 + NUM2;//4

Post Type:

var num1 = 1;
var num2 = 2;
var num3 = num1++ + NUM2;//3

The two pieces of code come up with different results, and the reason is that there is a very important difference between the descending increment and the preceding increment, that is, the post increment decrement operation is performed after the statement containing them is evaluated.

Scope of application:
The forward increment decreases and the back increments decrease all these four operators apply to any value. When applied to a different value, the operator converts the value as a number () transformation function, converting it to add and subtract 1.

2. Unary Plus and minus operators
unary plus and minus operators are primarily used for basic arithmetic operations, and can be used to convert data types (the operator converts this value like the number () transformation function).

Boolean operator
There are three Boolean operators: Non (not), with (and), or (or).

1. Logical Non-
the logical non-operator is represented by an exclamation mark (!) and can be applied to any value in the ECMAScript. This operator returns a Boolean value regardless of the data type of the value.

Using two logical non-operators at the same time, it actually simulates the behavior of a Boolean () transformation function

2. Logic and
the logic and operators are represented by two numbers (&&), have two operands, and can be applied to any type of operand. Logic and is a short-circuit operation, that is, if the first operand evaluates to False, the second operand is not evaluated.
The result is true when all two values are truth. The result is false when two values are one true false. Returns false when two values are false.
When one of these values is not a Boolean: follow these Rules

The first operand is false and the first is returned;

Returns the second when the first operand is true.
Returns the second operand if the first operand is an object

var a = {b:1};
A && ' ss '//"SS"

If the second operand is an object, the object is returned only if the first operand evaluates to True

' SS ' && a//object {b:1}

Returns the second operand if all two operands are objects

var c = {D:2};
C && a//object {b:1}

(1) returns null if one of the operands is null
(2) If one operand is Nan, return the Nan
(3) If one operand is undefined, return undefinded

3. Logical OR
similar to the logic and the operator, the logic or operator is also a short-circuit operator. In other words, if the first operand evaluates to true, the second operand will not be evaluated.

(1) The first operand is true and returns the first
(2) The first operand is false, and returns a second
multiplicative operator
the ECMAScript defines 3 multiplicative operators: multiplication, division, and modulo

Infinity*0//nan
0/0//nan
Infinity/infinity//nan

Additive operator
1. Addition (Turn string)
Two operators are numeric
Performs a conventional addition calculation.

Infinity +-infinity//nan

If one of the operands is a string

If two operators are strings, the second operator is spliced with the first operator
If only one operator is a string, the other operand is converted to a string, and then the two strings are spliced together.
If this operand is an object, numeric value, or Boolean value, call their ToString () method to get the corresponding string value, and then apply the previous rule about the string. For null and undefined, the string () function is called separately and the strings "undefined" and "null" are obtained.

2 + '//' 2 '

2. Subtraction (Turn value)
If all two operands are numeric
Performs a general arithmetic decrement operation and returns the result, if one operand is Nan, the result is Nan

Infinity-infinity//nan

If one operand is not a numeric value

If one operand is a string, Boolean, NULL, or undefined, the number () function is called in the background to convert it to a numeric value, and then a subtraction calculation is performed based on the preceding rule. If the result of the conversion is Nan, then the result of the subtraction is Nan.
If there is an operand that is an object, the valueof () method of the object is called to obtain a numeric value representing the object. If the resulting value is Nan, the result of the subtraction is Nan. If the object does not have a valueof () method, it calls its ToString () method and converts the resulting string to a numeric value.

5-true//4

Relational operators
performs a numeric comparison if all two operands are numeric
If two operands are strings, compare the character encoding values for the two strings
If one operand is a numeric value, the other operand is converted to a numeric value, and then a numeric comparison is performed

var result = ' $ ' < ' 3 '//true
var result = ' 3//false ' <

Equality operator
1. Equality and inequality
convert first and then compare

(1) If one operand is a Boolean, convert it to a numeric value before comparing equality
(2) If one operand is a string and the other operand is a numeric value, first converts it to a numeric value
(3) If one operand is an object and the other is not, the valueof () method of the object is invoked, and the resulting base type value is compared with the preceding basic rule
null and undefined are equal.
You cannot convert null and undefined to any other value before you want to compare equality
If all two operands are Nan, the equality operator returns false, and by rule, Nan is not equal to Nan

2. Congruent and not congruent
Compare and not convert only

"!=="//true

Conditional operator

Variable = boolean_expression? True_value:false_value 

In essence, the meaning of this code is based on the result of boolean_expression evaluation, decide what value to assign to the variable variable. If the value evaluates to TRUE, the variable is true_value, and if the value evaluates to FALSE, the variable variable is assigned a False_value value.

Assignment operator
A simple assignment operator is represented by an equal sign, and its function is to assign the value on the right to the variable on the left.

Comma operator
The comma operator is used to declare more than one variable, but in addition, the comma operator is also used to assign values. When used for assignment, the comma operator always returns the last item in the expression.

Related Article

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.