How to Use the javascript equal sign operator _ javascript skills

Source: Internet
Author: User
In JavaScript, the equal sign is represented by the double equal sign (). if and only when the two operations are equal, it returns true. Today, we will discuss in detail the problem of the equal sign operator, and attach an analysis of the differences between the equal sign operator and the full equal sign operator. This topic describes how to use the javascript medium number operator.

The most basic usage of the equal sign operator is to compare whether the two arithmetic operations are equal. See the following code example:

The Code is as follows:


Var a = 2, B = 2;
Console. log (a = B );

If the two operands are equal, the return value is true. Otherwise, false is returned.
The above is the most basic usage. Next we will introduce several special cases.

The Code is as follows:


Var str = "5 ";
Var num = 5;
Console. log (str = num );

The Return Value of the above Code is true.

Many may have questions: one is a string, the other is a numerical value, and how to return a true value. Of course, this is definitely wrong in c # or java, but in js, this is not a problem. Implicit data type conversion is performed, and strings are converted to numbers.
Let's take a look at a code example:

The Code is as follows:


Console. log (true = 1 );

The above Code also returns true, because true also performs implicit conversion, it will be converted to 1, false will be converted to false.
Next, let's look at a code example:

The Code is as follows:


Var obj = {
ValueOf: function () {return 10}
}
Console. log (obj = 10 );

An object can be compared with a number directly, and the return value is true.
This is because the object will first call the valueOf () method. If there is no such rule, try to call the toString () method.

The difference between two equal signs in javascript and three equal signs:

In javascript code, we often see the use of three equal signs and two equal signs. The following describes the differences between the two.
Code example:
Instance 1:

The Code is as follows:


Console. log (0 = "");
Console. log (0 = false );
Console. log ("" = false );

Example 2:

The Code is as follows:


Console. log (0 = "");
Console. log (0 = false );
Console. log ("" = false );

The above Code demonstrates the two operators. The following describes the differences between them.

Three equal signs all equal operators:

Since they are full equality operators, they must be identical to each other. The comparison rules are as follows:

1. If the two operations are of the value type, the two operands must be completely equal before they are equal.
2. If it is a reference type, the two operands must point to the same object to be equal.

Two equal sign operators:

This operator is more common. If the two operands have different types during the comparison, type conversion is performed. The specific rules are as follows:

1. If the data type is the same value type, the comparison rules are the same as the full-level operators.
2. If the two operands have the same type, they may also be the same:
A: If one is null and the other is undefined, the two are the same.
B: If one is a string and the other is a numerical value, convert the string to a numerical value before comparison.
C: If any value is true, convert it to 1 for comparison. If any value is false, convert it to 0 for comparison.
D: if one is an object and the other is a value or string, convert the object to a base type value for comparison. Converts an object to a base type and uses its toString or valueOf method.

The above is all the content of this article. I hope you will like it.

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.