JavaScript basics: Equals sign comparison

Source: Internet
Author: User
Tags object object true true

Includes the = = and = = = Operators, the former is lenient judgment equality, if the two types are different before the comparison value will be type conversion, the latter is a complete equality determination.

1, numeric, string, and Boolean values by value comparison

= = = = = Whether the two are identical before and after detection, only if they hold the same values exactly.

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

True True

var a = String (' a ');
var B = String (' a ');
Console.log (A==B,A===B);

True True

JS numbers are floating point number, there is no problem of integral type to floating point type

var a = 1;
var B = 1.0;
Console.log (A==B,A===B);

True True

var a = 1;
var B = true;
Console.log (A==B,A===B); = = before a type conversion, to a Boolean value

True False

2. Use reference comparisons between objects, arrays, and functions.

Arrays and functions are special forms of objects.

If objects have different storage addresses, they are not equal.

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

False false

var a = [2,3];
var B = A;
Console.log (A==B,A===B);

True True

3. Special Nan

Nan is not equal to any numeric, object comparison, including itself.

var a = 2;
var B = 0/0;
Console.log (B,a==b,a===b,b==nan);

NaN false False

4. Comparison of NULL and undefined

The comparison of these two basic types is the same as for numbers and strings.

var a = null;
var b = undefined;
var D;
Console.log (d,a==b,a===b,b===d,d===undefined);

Undefined true true false true

5. Comparing objects to strings

JS internally converts the object to a value of the original type, usually first trying the valueof method before attempting the ToString conversion.

var a = ' [Object Object] ';
var b = {};
Console.log (A==b,b.tostring ());

True [Object Object]

The only exception is the core object date in JS, which is converted directly using the ToString () method.

var a = new Date (2016,04,15);
var B = 1463241600000;
var c = ' Sun may 00:00:00 gmt+0800 ';
Console.log (A==b,a==c,a.valueof (), a.tostring ());

False true 1463241600000 Sun may 00:00:00 gmt+0800

JavaScript basics: Equals sign comparison

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.