JavaScript equals operator using detailed _javascript tips

Source: Internet
Author: User
Tags numeric value

This section describes the use of the Equals operator in JavaScript, and the friends you need can do the reference.

The most basic use of the equals sign operator is to compare the equality of two operands, looking at the following code example:

Copy Code code as follows:

var a=2,b=2;
Console.log (A==B);

If two operands are equal, the return value is true, or false.
The above is the most basic use, and then to introduce its relatively special several cases.

Copy Code code as follows:

var str= "5";
var num=5;
Console.log (Str==num);

The above code return value is true.

Many friends may have the question, one is the string, one is the numeric type, how returns the value is true, certainly this in C # or Java certainly is wrong, but in JS all this is not the question, will carry on the implicit data type conversion, the string will attempt converts to the numeral.
Let's take a look at some code examples:

Copy Code code as follows:

Console.log (true==1);

The code above also returns True, because True is also implicitly converted, and it will be converted to 1,false to be converted to false.
Let's look at a code example below:

Copy Code code as follows:

var obj={
Valueof:function () {return 10}
}
Console.log (obj==10);

An object can compare size with a direct number, and the return value is true.
This is because the object invokes the ValueOf () method first, and attempts to invoke the ToString () method if there is no such method.

The difference between the two equals sign and the three equals sign of javascript:

In the JavaScript code that often sees the use of three equals and two equals, here's what the difference is.
Code instance:
Example one:

Copy Code code as follows:

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

Example two:

Copy Code code as follows:

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

Your code above shows two operators, and here's a look at the difference between them.

Three equal equals equality operators:

Since it is the strict equality operator, then the two operands must be identical in exactly the same way, and the specific comparison rules are as follows:

1. If two operations are value types, then two operands must be exactly equal to be equal.
2. In the case of a reference type, two operands must point to the same object to be equal.

Two equals operator:

This operator is more philanthropic, and in comparison, if the type of two operands is different, the type conversion is done, as follows:

1. If the value type is the same as the data type, then the comparison rule and the strict equality operator are the same.
2. If the two operands are of the same type, they are likely to be the same:
A: If one is null and one is undefined, then two is the same.
B: If one is a string, and one is a numeric value, the string is converted to a numeric value and then compared.
C: If any value is true, convert it to 1 and compare it to 0 if any value is false.
D: If one is an object, and the other is a numeric value or a string, the object is converted to the underlying type for a comparison. object to the underlying type, using its ToString or ValueOf method.

The above is the entire content of this article, I hope you can enjoy.

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.