The difference between the equality (= =) operator and the equals (= = =) operator

Source: Internet
Author: User

First, understand the data types in JS:

JS data types can be divided into 2 categories, that is, the basic data types and reference types.

The underlying data types are divided into: Undefined, null, String type, Number type, Boolean type.

Reference types: Type Object (most reference types are instances of object, common reference types are: Array (array), date (s), REGEXP (Regular), function (functions), and so on).

Second, the difference between the two:

Personally, compared to the third edition of JavaScript Advanced Programming, the fourth edition of the JavaScript authoritative guide, this book will summarize the difference between the two better and more carefully, the following summary is mainly referred to the JavaScript authoritative guide Fourth edition.

the = = Equality operator and the = = = equals operator are used to detect whether two values are equal, equal to return true, and unequal return false;== is the equality operator, which uses a looser identity definition (that is, allow type conversions) to detect whether two operands are equal; = = = equals operator, It uses a strict identity definition to detect whether the two operands are identical.

(in the fourth edition of the JavaScript authoritative guide), = read as "assignment," = = read as "equals", read = = = "Equal", the individual feels that the difference is better to avoid confusing them)

the = = = operator Compares a rule with two values that are identical:

I summarize its rules into 2 categories of comparisons:

1. Comparison between two basic data types: a) If the two values are of different types, then two values are different (null and undefined are also of different types).

b) NaN (not a number) will never be equal to any other value including itself

Verify:

2. Comparison in two reference types (object, array, function): When two values refer to the same object, array, or function, they are identical. (The comparison of reference types = = = = = = = = = = = = = = = = = = = = = = = =) If these two values refer to not the same object, immediately their objects have exactly the same properties or arrays with exactly the same elements, and neither value is exactly equal.

Allocation of variable in-memory assignment reference type in memory

The direct comparison of two basic variables will directly compare the values stored in the two variables, the two reference types of the variables directly compared, compared to two variables of the memory address, each reference type variable will be allocated in the stack different memory address, their values are stored in the heap memory ; So for reference types, only two of the reference type's variables point to the same object, and they are both equal.

the = = operator Compares a rule that has two values equal:

= = is a little more complicated than = = when comparing the values of the base type, = = when comparing values of two different types, the values of the two variables are converted and then compared.

The rules are grouped into 2 categories :

1. Comparison between two basic data types: a) Two value types are the same, comparing the equivalence of two values, identical or equal;

b) Two values of different types, may still be equal: 1. String and numeric comparisons, the = = Operator First converts a string to a number, and then compares the two values;

2. A value of true, converted to 1, then comparison; a value of false, converted to 0, and then compared;

3.null==undefined equals (= = = = = = = = = = = = = = = =)

2. Comparisons in reference types:

About the use of ToString () and valueof () I'm here to analyze the array object as an example (probably not quite a whole, and then I want to add it later):

The reference type of JS

Summary: It is recommended to use the "= = =" Equivalent operator for comparison, and also to conform to JavaScript coding specifications.

The difference between the equality (= =) operator and the equals (= = =) operator

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.