JavaScript learning notes operator (continued), javascript learning notes

Source: Internet
Author: User

JavaScript learning notes operator (continued), javascript learning notes

I. Multiplication Operators

1. Multiplication :*

Special rules for multiplication operators:

If the operands are all numeric values, they are calculated by regular multiplication. If the product exceeds the range indicated by the ECMAscript value, infinity or-infinity is returned.
If one of the operands is NaN, the returned result is NaN.
If infinity is multiplied by 0, NaN is returned.
If infinity is multiplied by a non-zero number, infinity or-infinity is returned.
Returns infinity if infinity is multiplied by infinity.
If one of the operands is not a value, the backend will call number () to convert it to a value, and then apply the preceding rule.

Copy codeThe Code is 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 Division operators:

If the operands are all numerical values, they are calculated according to the regular Division. If the operator exceeds the range indicated by the ECMAscript value, infinity or-infinity is returned.
If one of the operands is NaN, the returned result is NaN.
If infinity is divided by infinity, NaN is returned.
If 0 is divided by 0, NaN is returned.
If a non-zero finite number is divided by 0, infinity or-infinity is returned.
Returns infinity or-infinity if infinity is divided by a finite number other than zero.
If one of the operands is not a value, the backend will call number () to convert it to a value, and then apply the preceding rule.

Copy codeThe Code is as follows:
<Script type = "text/javascript">
Alert (5/5); // 1
Alert (5/NaN); // NaN
Alert (Infinity/Infinity); // NaN
Alert (Infinity/2); // Infinity
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 all numerical values, the remainder of the Division is returned based on the regular Division calculation.
If the divisor is infinite and the divisor is finite, the returned result is NaN.
If the divisor is finite and the divisor is 0, NaN is returned.
If infinity is divided by infinity, NaN is returned.
If the divisor is finite and the divisor is infinite, the devisor is returned.
If the divisor is 0, 0 is returned.
If one of the operands is not a value, the backend will call number () to convert it to a value, and then apply the preceding rule.

Copy codeThe Code is 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>

Ii. Addition Operators

1. Addition OPERATOR: +

If one of the operands is a string:

If both operands are strings, splice the second operand to the end of the first operand.
If only one operand is a string, convert the other operand to a string and then execute the preceding rule.

Copy codeThe Code is as follows:
<Script type = "text/javascript">
Var result1 = 5 + 5; // Add a number
Alert (result1); // 10
Var result2 = 5 + "5"; // Add a number to a string
Alert (result2); // "55"
</Script>

2. subtraction OPERATOR :-

If one of the operands is a string, Boolean value, null, or undefined, call number () in the background to convert it to a value and then perform subtraction.

Iii. Relational operators

Greater than:>

Less than: <

Greater than or equal to:> =

Less than or equal to: <=

Special rules for Relational operators:

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

If one operand is a value, convert the other operand to a value before comparison.

The result of comparing any number with NaN is false.

IV,Equal Operators

1. Equal and not equal: = and! =

Both operators convert the operands to the same type before comparison.

During conversion, the equal and unequal operators follow the following rules:

If the type of an operand is Boolean, convert it to numeric type first, convert false to 0, and convert true to 1.
If one of the operands is a string and the other is a number, convert the string to a number for comparison.
If one of the operands is an object and the other is not, call the valueof () method of the operand to obtain the basic type value and then compare it.
Special rules for comparison:

Null and undefined are equal.
Null and undefined are not converted to any other type.
If the result of any operation is NaN, if the comparison is equal, false is returned. If the comparison is not equal, true is returned. Note: Even if both operands are NaN, the returned result is false, that is, NaN is not equal to NaN.
If both operands are objects, compare the referenced values. If the same object is referenced, true is returned. Otherwise, false is returned.
2. Full and incomplete: = and =

= Converts the operands to the same type for comparison;

=== No conversion type, direct comparison

For example:

Copy codeThe Code is as follows:
Var result1 = ("55" = 55 );
Var result2 = ("55" = 55 );
Alert (result1); // true
Alert (result2); // false "55" is a string, 55 is a number, and the type is different

5. Conditional Operators

Variable = conditional expression? True Value: false value

Evaluate the conditional expression first. If the result is true, the true value is assigned to the variable. If the result is false, the false value is assigned to the variable.

Copy codeThe Code is as follows:
<Script type = "text/javascript">
Var num1 = 10;
Var num2 = 25;
Var num3 = (num2> num1 )? Num2: num1;
Alert (num3); // 25
</Script>

Vi. assignment operators

1. Simple value assignment operator: =

Var num1 = 10;
Num = num1 + 10;
2. compound assignment operators: + =,-=, * =,/=, % =, >>=, <=, >>>=

Copy codeThe Code is 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>

VII. Comma Operator

The comma operator can execute multiple operations in one statement.

Purpose: 1. Declare multiple variables

Var num1 = 1, num2 = 2, num3 = 3;
2. Assignment

Var num = (0, 1, 2, 3) // num = 3
When used for value assignment, the comma operator always returns the value of the last expression.

Exercise area:

Copy codeThe Code is as follows:
<Script type = "text/javascript">
Var num1 = 5;
Var num2 = 10;
Var message = "The sum of 5 and 10 is" + num1 + num2;
Alert (message );
</Script>

Copy codeThe Code is as follows:
<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>

The above is all the content of this article. The explanation of the javascript operator is over. In the next article, we will explain the 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.