"X! = X" in JavaScript

Source: Internet
Author: User

In the application of JavaScript, it is often encountered to determine whether two objects/values are equal. Some indicate that they look the same, but they are not the same. There are some special cases that we need to identify.

Reference type

They are all reference types, and the stored space is allocated from the heap. The value at the variable is a pointer to the memory where the object is stored.

Two objects with the same properties.
var a = { x : 1 }var b = { x : 1 }a === b // false  两个单独的对象永不相等a == b // false  两个单独的对象永不相等

They are actually new Object() shorthand. When the variable A is initialized, an object value is assigned, which opens up a new space in the heap.
Initializing variable B also opens up a new space. Two variables, the memory address pointed to is not the same.

Two separate empty arrays.
var a = [], b = []a === b // false  两个单独的数组永不相等。a == b // false  两个单独的数组永不相等。

The above two examples have the same principle.

The original type determines whether the value of type number is Nan

ECMAScript (ES6) contains the Number.isnan () function.
Number.isNaN(x)to detect if the variable x is a non-numeric value. However, in the absence Number.isNaN of a function, it (x != x) is more reliable to detect if the variable x is through an expression NaN .

String type

String type, I do not currently find the same literal, will not be equal to the case.

Symbol type
const s1 = Symbol() const s2 = Symbol()s1 == s2 // falses1 === s2 // false

The symbol () function returns the value of the symbol type, is unique, is not equal, and has a static property and a static method.

Finish

"X! = X" in JavaScript

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.