Learn JavaScript from the beginning (v)--operator (ii)

Source: Internet
Author: User

Original: Learn JavaScript from the beginning (v)--operator (ii)

One, multiplicative sex operator

1. Multiplication: *

Some special rules for multiplication operators:

    • If the operand is a numeric value, it is calculated by normal multiplication, and if the product exceeds the representation range of the ECMAScript value, it returns infinity or-infinity
    • If one of the operands is Nan, the return result is Nan
    • If infinity is multiplied with 0, returns Nan
    • If the infinity is multiplied by a non-0 number, return infinity or-infinity
    • Infinity is multiplied by infinity and returns infinity
    • If an operand is not a numeric value, the background calls number () to convert it to a number, and then apply the rule above
1<script type= "Text/javascript" >2     3Alert (5 * 6);// -4Alert (5 * NaN);//NaN5Alert (Infinity * 0);//NaN6Alert (Infinity * 2);//Infinity7Alert ("5" * 5);// -8Alerttrue* 10);//Ten9Alertfalse* 10);//0Ten         One</script>

2, Division:/

Some special rules for division operators:

    • If the operand is a numeric value, according to the general division calculation, if the quotient exceeds the representation range of the ECMAScript value, it returns infinity or-infinity
    • If one of the operands is Nan, the return result is Nan
    • If Infinity is infinity, return Nan
    • If 0 is removed by 0, return Nan
    • If a finite number of non-0 is removed by 0, return Infinity or-infinity
    • If Infinity is a finite number other than 0, return infinity or-infinity
    • If an operand is not a numeric value, the background calls number () to convert it to a number, and then apply the rule above
1<script type= "Text/javascript" >2     3alert (5/5);//14alert (5/nan);//NaN5alert (infinity/infinity);//NaN6alert (INFINITY/2);//Infinity7alert (5/0);//Infinity8Alert (10/true);//Ten9Alert (10/false);//InfinityTen         One</script>

3. Modulus (remainder):%

Some special rules for the modulo operator:

    • If the operands are numeric, the remainder is returned in accordance with the normal division calculation
    • If the divisor is infinite and the divisor is a finite number, the return result is Nan
    • If the divisor is a finite size, the divisor is 0, which returns Nan
    • If Infinity is infinity, return Nan
    • If the divisor is finite and the divisor is infinity, return dividend
    • If the divisor is 0, return 0
    • If an operand is not a numeric value, the background calls number () to convert it to a number, and then apply the rule above
1<script type= "Text/javascript" >2     3Alert (26 5);//14Alert (Infinity% 3);//NaN5Alert (3 0);//NaN6Alert (5 Infinity);//57Alert (0 10);//08Alerttrue% 25);//19Alert (3false);//NaNTen         One</script>

Second, additive operator

1. Addition Operator: +

If one of the operands is a string:

    • If the two operands are strings, the second operand is stitched to the back of the first operand.
    • If only one operand is a string, then a second operand is converted to a string before the rule is executed
<script type= "Text/javascript" >            var result1 = 5 + 5;     // numbers plus numbers        alert (RESULT1);           // Ten        var result2 = 5 + "5";   // numbers plus strings        alert (RESULT2);           // "the"           </script>

2, subtraction operator:-

If an operand is a string, Boolean, NULL, or undefined, then number () is called in the background to convert it to a numeric value, and then the subtraction is performed.

Third, relational operators

Greater than:>

Less than:<

Greater than or equal to: >=

Less than equals: <=

Special rules for relational operators:

If the operand is a string, compare the corresponding character encoding of two strings

If the operand is a numeric value, then the other operand is converted to a numeric value before being compared

Any number compared to Nan, the result is false

Four, equality operators

1, equal and unequal: = = and! =

Both operators will first convert the operands to the same type and then compare

When converting, the equality and inequality operators follow the following rules:

    • If one of the operands is of type Boolean, first convert it to a number type, false to 0, and true to 1.
    • If the type of one of the operands is a string and the other is a numeric type, then the string is converted to a number for comparison.
    • If one of the operands is an object and the other is not, then the valueof () method of the operand is called first to obtain the base type value before comparing

Special rules when comparing:

    • null and undefined are equal.
    • Null and undefined are not converted to any other type
    • If the result of any one operation is NaN, then the equality comparison returns false, and the inequality comparison returns true. Note that even if the two operands are Nan, the returned result is false, that is, Nan is not equal to Nan.
    • If the two operands are objects, then the values they reference are compared, and if the same object is referenced, the return is true, otherwise, false is returned.

2, congruent and not congruent: = = and = = =

= = Converts the operand to the same type comparison;

= = = does not convert type, direct comparison

For example:

1 var = ("result1" = =);     2         var = = ("result2" = ==3         alert (RESULT1);   true4         //false "55" is a string, 55 is a number, the type is different

V. Conditional operators

variable = conditional expression? Truth value: false values

The conditional expression is evaluated first, and if the result is true, the truth is assigned to the variable, and if False, the value is assigned to the variable.

1 <script type= "Text/javascript" >2             3         var num1 = ten; 4         var num2 =; 5         var num3 = (num2 > Num1)? num2:num1; 6         alert (num3);   //  - 7 8     </script>

Six, assignment operators

1. Simple assignment Operator: =

var num1 = ten; num=num1+10;

2. Compound assignment operator: + =,-=, *=,/=,%=, >>=, <<=,>>>=

1<script type= "Text/javascript" >2     3         varnum = 5;4alert (num);//55         6num + = 5;7alert (num);//Ten8         9Num *= 2;Tenalert (num);// - One  ANum/= 10; -alert (num);//2 -          thenum-= 2;  -alert (num);//0 -          -          +</script>

Seven, comma operator

The comma operator can perform multiple operations in a single statement

Purpose: 1, declare multiple variables

var num1=1,num2=2,num3=3;

2. Assign Value

var num= (0,1,2,3)//num=3

When used for assignment operations, the comma operator always returns the value of the last expression.

Practice Area:

1  <script type= "Text/javascript" > 2              3         var num1 = 5; 4         var num2 = ten; 5         var message = "The sum of 5 and Ten is" + NUM1 + num2; 6         alert (message);   7    </script>
<script type= "Text/javascript" >Alert (NULL==undefined); Alert (NULL===undefined); Alert ("NaN" = =NaN); Alert ("NaN" = = =NaN); Alert (NaN==NaN); Alert (NaN===NaN); Alert (NaN!=NaN); Alert (NaN!==NaN); Alert (false= = 0); Alert (false= = 0); Alert (true= = 1); Alert (true= = 1); Alert (NULL= = 0); Alert (Undefined= = 0); Alert (5 = = "5"); Alert (5 = = = "5"); </script>

If you want to know the answer, please leave a message in the comment area ~

Learn JavaScript from the beginning (v)--operator (ii)

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.