JavaScript Learning Note Operator (cont.) _ Basics

Source: Internet
Author: User
Tags comparison numeric numeric value

One, multiplicative operator

1, Multiplication: *

Some special rules for multiplication operators:

If the operands are numeric, the normal multiplication calculates that if the product exceeds the representation range of the ECMAScript value, return infinity or-infinity
If one operand is Nan, then the return result is Nan
If infinity is multiplied by 0, return Nan
If the infinity is multiplied by a non 0 number, return infinity or-infinity
Infinity is multiplied with infinity to return to infinity
If there is an operand that is not a numeric value, the background will call number () to convert it to a numeric value, and then apply the rules above

Copy Code code as follows:

<script type= "Text/javascript" >

Alert (5 * 6); 30
Alert (5 * NaN); NaN
Alert (Infinity * 0); NaN
Alert (Infinity * 2); Infinity
Alert ("5" * 5); 25
Alert (True * 10); 10
Alert (FALSE * 10); 0

</script>

2, Division:/

Some special rules for the division operator:

If the operands are numeric, the normal division calculates that if the quotient exceeds the range of ECMAScript values, return infinity or-infinity
If one operand is Nan, then the return result is Nan
If it is infinity by infinity, return Nan
If 0 is removed by 0, return Nan
If a finite number of 0 is removed by 0, return Infinity or-infinity
If the infinity is removed by a finite number not 0, return infinity or-infinity
If there is an operand that is not a numeric value, the background will call number () to convert it to a numeric value, and then apply the rules above

Copy Code code as follows:

  <script type= "Text/javascript" >
          alert (5/5);          //1
         alert (5/nan);        / /nan
         alert (infinity/infinity);   //nan
& nbsp;        alert (INFINITY/2);   //Infinity
  & nbsp;      alert (5/0);          // Infinity
         alert (10/true);       //10
         alert (10/false);     // Infinity
     </script>

3. Modulus (remainder):%

Some special rules for modulo operators:

If the operands are numeric, the remainder is returned in accordance with the usual division calculation.
If the divisor is infinity and the divisor is a finite number, the return result is Nan
If the divisor is a finite size, the divisor is 0, and returns Nan
If it is infinity by infinity, return Nan
If the divisor is finite and the divisor is infinity, return dividend
If the divisor is 0, return 0
If there is an operand that is not a numeric value, the background will call number () to convert it to a numeric value, and then apply the rules above

Copy Code code as follows:

<script type= "Text/javascript" >

Alert (26% 5); 1
Alert (Infinity% 3); NaN
Alert (3% 0); NaN
Alert (5% Infinity); 5
Alert (0% 10); 0
Alert (true% 25); 1
Alert (3% false); NaN

</script>

Second, additive operator

1. Addition Operator: +

If one of the operands is a string:

If two operands are strings, the second operand is spliced after the first operand.
If only one operand is a string, then the other operand is converted to a string and then the preceding rule is executed

Copy Code code as follows:

<script type= "Text/javascript" >
var result1 = 5 + 5; Numbers plus numbers
alert (RESULT1); 10
var result2 = 5 + "5"; Number Plus string
alert (RESULT2); "55"
</script>

2, subtraction operator:-

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

Third, the relational operator

Greater than:>

Less than:<

Greater than or equal to: >=

Less than or equal: <=

Relationship operator Special rules:

If the operand is a string, the corresponding character encoding is compared to two strings

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

Any number compared to Nan, the result is false

Four, the equality operator

1, equal and not equal: = = and! =

Both operators convert the operands to the same type before comparing

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

If the type of one operand is Boolean, first converts it to a numeric type, false to 0, and true converts to 1.
If the type of one operand 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, the valueof () method of the operand is called first, then the base type value is then compared
Special rules for Comparisons:

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 return result is false, that is, Nan is not equal to Nan.
If two operands are objects, compare the values they refer to, and if the same object is referenced, return true, otherwise, return false.
2, congruent and not congruent: = = and = =

= = converts operands to the same type of comparison;

= = = does not convert type, direct comparison

For example:

Copy Code code as follows:

var result1 = ("55" = = 55);
var result2 = ("55" = = 55);
alert (RESULT1);//true
alert (RESULT2); False "55" is a string, 55 is a number, the type is not the same

Five, conditional operator

variable = conditional expression? True truth: False value

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

Copy Code code as follows:

<script type= "Text/javascript" >
var num1 = 10;
var num2 = 25;
var num3 = (num2 > Num1)? NUM2:NUM1;
alert (NUM3); 25
</script>

Six, assignment operator

1. Simple assignment Operator: =

var num1 = 10;
num=num1+10;
2. Compound Assignment operators: + =, =, *=,/=,%=, >>=, <<=, >>>=

Copy Code code as follows:

<script type= "Text/javascript" >
var num = 5;
alert (num); 5
num + + 5;
alert (num); 10
Num *= 2;
alert (num); 20
Num/= 10;
alert (num); 2
num-= 2;
alert (num); 0
</script>

Seven, the comma operator

The comma operator can perform multiple actions in a single statement

Purpose: 1. Declare multiple variables

var num1=1,num2=2,num3=3;
2. Assigning 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:

Copy Code code as follows:

<script type= "Text/javascript" >
var num1 = 5;
var num2 = 10;
var message = "The sum of 5 and are" + NUM1 + num2;
alert (message);
</script>

Copy Code code as follows:

<script type= "Text/javascript" >
Alert (Null = = undefined);
alert (null = n = 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>

The above is the entire content of this article, for JavaScript operators to explain the end of this, the next article we will explain JavaScript statements.

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.