JavaScript = = and = =, and if (XXX) Summary

Source: Internet
Author: User

Reprint please indicate this article from: http://blog.csdn.net/nancle

First say = = and = = =

  • First, a very special value of Nan, typeof (Nav) gets ' number ', but Nan does not equal no matter what a value (including itself). Infer whether a value is Nan and can only use isNaN
    Nan = = nan//Get Falsenan = = = Nan//Get Falseisnan (Nan)//Get True
  • then = = = also simpler:
    1. equals both types and values are equal to return true
      1 = = = 1//Get true1 = = = ' 1 '//Get False
    2. For objects, arrays, functions (all three are object instance): Only the same object is referenced on both sides of the equal sign. To return True. referencing different objects. Returns false even if the value of two objects is equal.
      var a = {test: ' test '};var b = {test: ' test '};var c = A;a = = = b//Get Falsea = = = C  //Get True
    3. For undefined and null: only if both sides are undefined at the same time or null at the same time are completely equal
      undefined = = = undefined//get truenull = = = NULL//Get trueundefined = = = NULL//Get FALSE
  • = = Slightly more complicated:
    1. Assume that the type and value are equal on both sides of the equals sign returns True
    2. The
    3. assumes equal types on both sides of the equals sign. Values are not equal, however, the following rules are compared:
      • assumes that a value is a number. There is also a value of string that converts the string to a number. Then compare the converted values.

        1 = = ' 1 '//Get true 
      • assume a value of true, convert it to 1, and then compare it. The

        assumes a value of false, converts it to 0, and then makes a comparison.

        true = = 1//Get TRUE0 = = false//get true 
      • assume that both sides of the equals sign are objects, Then you must refer to the same object to return True. Suppose a value is an object, and a value is either a number or a string. Converts the object to a value of the original type, and then the comparison. The ability to use the ToString () method of an object or the valueof () method to convert an object to a value of the original type. The inner class of the

        JavaScript core language typically attempts the valueof () method transformation before attempting the ToString () method transformation, but for the date class. Run the ToString () method before running the valueof () method transformation. Objects that are not part of the JavaScript core language can convert themselves to original values in a way that is defined by the JavaScript implementation.

        var a = {test: ' test '};var b = {test: ' test '};a.valueof = function () { return 1};b.valueof = function () {return 1};a = = b//return FALSE1 = = A//returns True 
    4. For undefined and null: Both sides of the equals sign return true if they appear arbitrarily
      undefined = = undefined//Get truenull = = NULL//Get trueundefined = = NULL//Get True

Then explain if (XXX):

if (XXX) is actually very easy, it always converts the value of XXX to a Boolean (that is, true or false); 0, ' 0 ', [],null,undefined,nan converted to Boolean type are false.


Attached: Here is a very practical technique. Add two in front of the variable!! Take the inverse symbol. The ability to cast a variable into a Boolean type, in this way,!! NaN = = =!! Null also gets true, wonderfully!

JavaScript = = and = =, and if (XXX) Summary

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.