The difference between = = (equality operator) and = = = (Strict equality operator) in Javascript

Source: Internet
Author: User

In js, "= = =" is called the strict operator, "= =" is called the equality operator.

The difference is that the equality operator ( == ) compares two values for equality, and the strict equality operator ( === ) compares whether they are "the same value".

If two values are not of the same type, the strict equality operator ( === ) returns directly false , whereas the equality operator ( == ) converts them to the same type and then compares them with the strict equality operator.

The strict operator rules are as follows:

(1) different type values return false directly if the two values are of different types.

18 = = = "18"; // false true; // false
1===1; True

(2) When comparing values (numeric, String, Boolean) of the original type of the same type with the original type value of the same class, the value returns True, and False if the value is different.

Nan = = = Nan;  // false // nan is not equal to any value, (including itself) // true

(3) When comparing the data of a compound type of the same class with two composite types (objects, arrays, functions), it is not compared to whether their values are equal, but rather whether they point to the same object.

// false // false (functionfunction//  false
var v1 = {}; var v2 =//  true// Two variables refer to the same object, they are equal

(4) Undefined and null undefined and null are strictly equal to themselves.

// true NULL null; // true
var v1;var v2;v1 === v2; // true
The default value is after the declaration of a variable undefined , so two variables that only declare unassigned are equal.

The equality operator is exactly the same as the strict equality operator when comparing data of the same type.

When comparing different types of data, the equality operator first converts the data into type conversions and then compares them with strict equality operators. The type conversion rules are as follows:

(1) The original type of the value of the original type of data is converted to a numeric type before being compared. Both the string and the Boolean value are converted to numeric values.

1 = =true;//true//equivalent to 1 = = = 10 = =false;//true//equivalent to 0 = = = 02 = =true;//false//equivalent to 2 = = = 12 = =false;//false//equivalent to 2 = = = 0' true ' = =true;//false//equals number (' true ') = = = Number (true)//equivalent to NaN = = = 1"= = 0;//true//equal to Number (") = = = 0//equivalent to 0 = = = 0"' = =false;//true//equals Number (") = = = Number (false)//equivalent to 0 = = = 0' 1 ' = =true;//true//equal to Number (' 1 ') = = = Number (true)//equivalent to 1 = = = 1' \ n 123 \ = = 123;//true//when the string is converted to a number, the preceding and trailing spaces are omitted

(2) When comparing objects with primitive type values (meaning generalized objects, including numbers and functions) compared to the values of the original type, the object is converted to the value of the original type and then compared.

(3) Undefined and null undefined and null compare with other types of values, the result is false, they compare with each other when the result is true.

false NULL // false false // false NULL // false // false  null//  true

Special reminder: Disadvantages of the equality operator

1. The type conversions that are hidden by the equality operator can lead to some counterintuitive results.

' = = ' 0 ';//false0 = = ";//true0 = = ' 0 ';//true2 = =true;//false2 = =false;//falsefalse= = ' false ';//falsefalse= = ' 0 ';//truefalse= = undefined;//falsefalse==NULL;//falseNULL= = undefined;//true' \t\r\n ' = = 0;//true

2. Using the equality operator may have unintended effects on subsequent code.

var a =ifnull) {         console.log (//1   var a =ifnull) {         console.log (// no output } // improper use, the execution intention changes.

Summary: It is recommended to use strict equality operators as much as possible.

Recommend Nanyi's article: HTTP://JAVASCRIPT.RUANYIFENG.COM/GRAMMAR/OPERATOR.HTML#TOC9

The = = (equality operator) and = = = (Strict equality operator) differences 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.