10 JavaScript-related secrets and quirks (1)

Source: Internet
Author: User

Data Types and definitions

1. Null is an object.

Among many JavaScript types, there is a Null type, which has a unique value of null, that is, its literal value, defined as a value that has no meaning at all. It looks like an object, and the following detection code:

  1. Alert (typeof null); // the 'object' is displayed'

As follows:

Although the typeof value is displayed as "object", null is not considered as an object instance. You must know that values in JavaScript are all Object instances. Each value is a Number Object, and each Object is an Object. Because null has no value, it is obvious that null is not an instance of anything. Therefore, the value below is equal to false.

  1. Alert (null instanceof Object); // false

Note: null is also interpreted as an object placeholder.

2. NaN is a numerical value.

NaN is intended to indicate that a value is not a numerical value, but it is a numerical value and is not equal to itself. It is strange to see the following code:

  1. Alert (typeof NaN); // The 'number' dialog box is displayed'
  2. Alert (NaN = NaN); // false

The result is as follows:

Actually, NaN is not equal to anything. Make sure that only isNaN can be used for a certain item.

3. arrays without keywords are equivalent to false (about Truthy and Falsy)

The following is another of the best JavaScript geeks:

  1. Alert (new Array () = false); // true

The result is as follows:

To know what happened here, you need to understand the concept of truthy and falsy. They are a true/flase literal. In JavaScript, all non-Boolean values will have a built-in boolean flag. When this value is required to have a boolean behavior, this built-in Boolean value will appear, for example, if you want to compare it with a Boolean value.

Because Apple cannot be compared with pear, when JavaScript requires two different types of values to be compared, it will first weaken it to the same type. False, undefined, null, 0, "", NaN are all weakened to false. This type of forced conversion does not always exist, only when used as an expression. Let's look at the following simple example:

  1. Var someVar = 0;
  2. Alert (someVar = false); // display true

The result is as follows:

In the above test, we tried to compare the values 0 and boolean values false. Because the data types of the two are incompatible, JavaScript will automatically convert them to the same truthy and falsy, 0 is equivalent to false (as mentioned above ).

You may have noticed that there are no empty arrays in the values equal to false above. The empty array is a strange trick: it actually belongs to truthy, but when the empty array is compared with the Boolean type, its behavior is falsy. Why? This is the reason. First, let's take an example to verify the strange temper of the empty array:

  1. Var someVar = []; // empty array
  2. Alert (someVar = false); // The result is true.
  3. If (someVar) alert ('hello'); // execute the alert statement, so someVar is treated as true.

The result is as follows:

Note: According to the author, the array has a built-in toString () method. For example, when alert is used directly, a string is popped up in the form of join, an empty array is a null string, which is equivalent to false. For more information, see the author's article Twisted logic: understanding truthy & falsy. However, I am surprised that, for example, null objects and empty functions, false is displayed when weak values are true or false. Why? Is it really because the array is a freak that requires special consideration?

To avoid the comparison problem of forced conversion, you can use strong equals (=) instead of weak equals (= ).

  1. Var someVar = 0;
  2. Alert (someVar = false); // The result is true-0 and belongs to falsy.
  3. Alert (someVar = false); // The result is false.-zero is a value, not a Boolean value.

The result is as follows (win7 FF4 ):

If you want to dig deeper into some of the special tricks of type forced conversion in JavaScript, see Official documentation specifications: section 11.9.3 of the ECMA-262

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.