JavaScript you don't know--data type

Source: Internet
Author: User

1. Data type

There are 7 data types in JS

6 of these are basic types including null, Undefined, Boolean, number, string and symbol, and a reference type Object

But when judging the data type, you can use typeof except for null

1. Null

How should null be judged?

typeof ' Object ' &&!a

2) undefined

Undefined judge can use typeof,

There is an interesting phenomenon, and few people are concerned about undeclared it, in fact undeclared (undeclared) and declaration but undefined (undefined) are two different phenomena,

But in JS the pit is undeclared and undefined is the same, so do not know the declaration of the variable or not

Fortunately, if a variable is not declared, it can be used to cause error in strict mode to judge a undeclared variable to prevent error.

For example, to indicate that a is not declared a can write a

2. Arrays and Arrays of classes

1. An array of classes refers to a class of objects that iterate, have values as attributes, and most importantly they have the length property, so the string is also an array of classes.

2. The judgment of JS array can not be used typeof but there is a new discriminant method for the object instanceof

3. An array is an object so you can write a[' foo '] = ' bar ' Also, but it does not increase his length, but the interesting point is that a["" "=" for "will cause length to become 14, although the incoming 13 is a numeric value

4. Class array conversion arrays can be used

Array.  from

can also

Array.prototype.slice.call (Fakearr)

Array.from is a fairly powerful thing, and slice is a more interesting implementation.

5. The method of borrowing an array is a very interesting thing, in the JS inside the array has the super rich Master, there are many practical tools, the string is very poor, so the string can be borrowed from the array oh, how to borrow it, like the above call that look.

3. Values

The relative knowledge of numerical value is quite unpopular, but it's also interesting.

1. has been criticized by the people must be JS there is no real integer, very classic one is

0.2 0.1 0.30000000000000004

is because JS internal storage is not integral type, floating point number is so differentiated,

2. Using E to represent the value is a very uncommon practice, it is estimated that the calculator is not enough to show that, JS is also possible oh, like 3.04e3 is equal to 3040

3. The interesting point of the value is that 42.toFixed (3) is a wrong syntax, because the point behind 42 is interpreted as a decimal point because 42. is also a correct way of expression, so 42. ToFixed (3) There's nothing wrong with that.

4.16 binary value is in front plus 0x and 8 binary value is in front plus 0

5. Nan is not to say that the data is not a numerical value, more appropriately a necrotic value, such as 2/' A ' is a necrotic value. But

typeof 2/'a' = = = ' number'  //True

At the same time JS has a global function IsNaN,

' a ' = = = NaN//False
IsNaN ('a') // true

6. +0 and-0 are numerically the same in JS, but are not the same from the angle of the vectors.

7. New method of ES6 object.is

Object.  is (2'a',  // true

4. Values and references

There are a lot of people who are not clear in the cliché of reference types and basic types.

1. In JS there is no pointer to the concept, and JS does not have a variable between the reference, it is possible that a value is referenced by 10 variables, but they are not related to each other

2. The copy of the base type is assigned by the value referenced, and the reference type is copied by the reference itself rather than by reference to each other. So there is this phenomenon.

varA = [1,2,3];varb =A;b.push (4)//1, 2, 3, 4B//[1, 2, 3, 4];A//[1, 2, 3, 4];

b = [1,2,3,4];b.push (5) b//[1, 2, 3, 4, 5];A//[1, 2, 3, 4];

The upper code indicates that when B has changed a reference value and a is not related, so after the change a does not change

JavaScript you don't know--data type

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.