JS data type and type conversion

Source: Internet
Author: User

I. Data type in JS data type:      5 basic type:undefined,null,boolean,number,string      1 Compound Types: Object (objects include arrays, functions, etc.)  1. The string string      string is a set of immutable ordered sequences of 16-bit values, A sequence of characters enclosed by single or double quotation marks.       The length of the string is the number of 16-bit values it contains       The string can be escaped by \ Backslash  2. Digital number      Numbers are integers and floating point      infinity represent infinite numbers, which means that the upper limit of the number that can be represented by JS is exceeded      nan represents a non-numeric value, and Nan is not equal to any other value. including its own  3. Boolean values boolean      Boolean values are composed of true and false two values, and true and false are reserved words in JS.      4.Undefined     undefined is a value consisting of Undefined, usually used to denote an undefined or ' value vacancy '.  5.Null     null is made up of null values, which are typically used to denote ' null ', perform a typeof operation on NULL, and return ' object ', so normally Null is generally considered a reference to an empty object.  6. Object object      objects are containers for properties, where each property has a name and value. Object types include objects, arrays, and functions.   Two. Type conversion display conversions: Use String (), Number (), Boolean (), object () to convert data into strings, numbers, Booleans, objects, and so on, also known as casts, using the following:
String (123)//"123" number (' 123 ')//123,number (' fddas4234 ')//nanboolean (0)//falseboolean ("//trueboolean") Trueobject (3)//new number (3)
and except for null and undefined, any other value has ToString (), usually the ToString () method and the string () method return the same result! The parseint () and parsefloat () methods convert the data into integers and floating-point numbers respectively, and the parseint () method can also accept the second parameter, which represents the cardinality of the conversion: parseint (11, 2)//3 and two conversion methods for the object, The first is the ToString () method, which returns: ({a:1}). ToString ()//"[Object Object]" The second method is ValueOf (), in most cases, the valueOf () method simply returns the object itself: ({a:1}) . VALUEOF ()//{a:1} implicit conversions: In JS, in many cases, some operators implicitly type-convert the operands, which is also a lot of interviewers like in the interview questions of the key content such as:
1 + ' 1 '/' one ' 2-' 1 '//1+ ' + '//32!0//truevar a = 2; a++//2var a = 2; ++a//3

In JS,-*/will convert the operand to a number, but note that the + is special, because in JS, + is not only a numeric operator, is also a string connector, so in the presence of the plus sign should be based on the specific circumstances to see the results, if the + numbers are both ends of the number, then the result is also a number, The same ends are strings, then the result is also a string, such as one end is a number, one end is a string, then the number is first converted to a string, the connection, so the result is a string:

1 + 1//2 ' 1 ' + ' 2 '//' 12 ' 1 + ' 1 '//' 11 ' 2 ' + 1//' 21 '

  

It is also important to note that at the same time multiple numbers are computed in the same order as the operator's precedence:
' 12 ' + 1 * 2//' 122 ' 12 + 1 + ' 2 '//' 132 '
In particular, it is important to note that the equality comparison operator = = Also implicitly converts the operand, and = = = does not convert the type:
0 = = False//true ' 1 ' = = 1//true1 = = = ' 1 '//false

Type detection: typeof operator: typeof return: Undefined,boolean,number,string,object,function. 6 values typeof the null operation is to return object, because null is generally considered a reference to an empty object, so it returns object when an array is typeof, which also returns object when it encounters the need for type detection of an array. Obviously it is not possible to use the TypeOf operator, which is the toString () method of the object that we can use: Object.prototype.toString.call ([up])//"[Object Array]" So we can simply encapsulate a function that detects if it is an array:
function IsArray (array) {     return Object.prototype.toString.call (array). Slice (8) = = = ' array ';} The IsArray () method returns True or Falseisarray (123)//falseisarray ([up])//true

  

Similarly, it can be encapsulated as a type detection function:
function type (array) {     return Object.prototype.toString.call (array). Match (/\w+/g) [1];} Type () returns a specific type of "String", "number", "Object", "Function", "Boolean", "Null", "Undefined", "Array", and so on (NaN)//"number" Type (undefined)//"Undefined" type ([up])//"Array" type (' SDFSD ')//"String" type (NULL)//"null" type ({a:2})//"Object" Type (function () {})//"function" type (TRUE)//"Boolean"

  

Instanceof Operator: The instanceof operator is used to detect whether the given data is of the given type: [] instanceof Array//true But for strings, the original value of the number is detected in instanceof always return false
' DSA ' instanceof string//falsenew String (123) instanceof String//true

  

When there is another way to do this, you can use the. constructor to detect its constructors in a way that allows type detection:
"FDSFF". Constructor = = String//true

  

JS data type and type conversion

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.