Clarify the tangled relationships in JavaScript that equals (= =) and congruent (= = =)

Source: Internet
Author: User
Tags numeric value

The article mainly combs the different types of values in the judgment is equal, the existence of a variety of special relationships, and give a reliable way to determine whether or not equal.

First of all, the two relations: equal to not necessarily congruent, congruent must be equal to, does not mean that must not be congruent, not congruent is not necessarily equal. In the article, can use congruent place, equal is also certain set up, equal to not set up place, congruent also certainly not set up, I believe we all can understand, no longer make special explanation.

judge the symbol for 0

0 = = 0, but they are not exactly the same, how to judge.

Let's look at a couple of relationships:

Infinity = = Infinity//trueinfinity = =-infinity//false1/-0 = =-infinity//true1/0 = = Infinity//True

So there are:

1/-0 = = 1/-0//true1/0 = = 1/0//true1/-0 = = 1/0//false

So, 0 is actually signed, you can use the above methods to judge. judge undefined and null

Let's look at a few sets of relationships:

undefined = = undefined//Truenull = = NULL//Truenull = = undefined//Truenull = = undefined//False

So, if you only judge whether a variable value is null or if the variable is undefined, just use "= =", but if you want to distinguish between null and undefined clearly, you need to make a further comparison. Here are two ways to determine null and undefined:

Object.prototype.toString.call (NULL) = = "[Object, NULL]" Object.prototype.toString.call (undefined) = = "[Object, Undefined] "//There is a relationship. Note that some of the questions will ask: typeof null = =" Object "typeof Undefined = =" Undefined "
Judging Regular Expressions

Two identical regular expressions are not equal in fact:

var a =/[1-9]/;var b =/[1-9]/;a = = b//False

Because A,b is actually a two regular expression object, it is also a reference type:

typeof a = = = "Object"//truetypeof b = = "Object"//True

If we want to be able to compare the contents of two regular expressions without caring for memory addresses, we need to compare the equality of two expression strings:

var a =/[1-9]/;var B =/[1-9]/; ' + a = = ' + b//True Note: ' +/[1-9]/= = '/[1-9]/'
Comparison of Strings

Here you need to differentiate between string and string objects

If it is a string, you can use the "= = =" symbol to judge it directly:

var a = ' a string '; var b = ' A string '; a = = = B//true

However, for a string object (reference type), the direct contrast is still the memory address:

var a = new string (' a string '); var b = new String (' a string '); a = = b//False

If you are concerned about whether the string content is the same, you can convert the string object to a string and compare:

var a = new string (' a string '); var b = new String (' a string '); ' + A = = ' + b//true//can also be compared using the ToString method: a.tostring () = = = B.tostring ()//True

So, the most reliable way to determine whether two strings are the same is the following:

function Isstringequal (A, b) {return ' + a = = = ' + b;}
Comparison of Numbers

You also need to differentiate between numeric and numeric objects:

var a = new number (5); var B = new (5);//Direct contrast when unequal a = = B//false//use + symbol to convert to numerical contrast +a = = +b//true

However, there is a special value that must be treated, that is, Nan, which is also the number type

Object.prototype.toString.call (Nan)//"[Object number]" typeof NaN//"number"

At the same time, its relationship with the following results in an exception to the method for determining whether the values are equal:

Nan = = Nan//false+nan = +nan//False Note: +nan or Nan

How do we judge the equality of the two when two values are Nan? Look at a proposition: for any non-Nan numeric object or value (a), +a = = +a always set up, if the equation is not established, then A is Nan. So, if you know that A is Nan and how it is Nan in B, then you want to judge the two to be equal.

if (+a!== +a) return +b!== +b;

Explained as follows:

Assuming A is Nan, the judgment condition is established, and if B is Nan, the expression of the return statement is set to return true, indicating that both are equal (both are Nan); if B is not Nan, the return statement's expression is not valid, and returns FALSE, indicating that the two are not equal.

The logic of the above judgment is integrated into a judgment function, namely:

function Isnumberequal (A, B) {if (+a!== +a) return +b!== +b;//handling special case return +a = = +b;}

Date Object Contrast

Object can not be directly used to judge the equals sign, we still only care about whether the date value is the same, so convert the date to a millisecond value and then compare the values to the same

var a = new Date (' 2017-9-7 '); var b = new Date (' 2017-9-7 '); a = = b//false+a = = +b//true Note: The value of +a is 1504713600000, that is, the corresponding 2017.9.7 The millisecond effect of 00:00:00 is the same as using the GetTime () method a.gettime () = = B.gettime ()//True

Comparison of Boolean objects

Objects cannot be directly compared, so convert the Boolean value to a numeric value, and then compare

var a = new Boolean (' 123 '); var B = new Boolean (' 123 '); a = = b//false+a = = +b//true//Note: Boolean is true, preceded by "+", converted to 1, false to 0

A lot of comparison methods, in fact, can find the corresponding object method to achieve the comparison of values, the main thing in this article is to remind you, in contrast, pay attention to distinguish between the value or object, if the goal is to the ratio is equal to further conversion. In addition, special attention is paid to the special circumstances in which the values of each type are compared and the relationship between the special values. >Web front-end Sharing group "189394454" free access to 2018 of the latest Web front-end learning materials.

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.