JS data type

Source: Internet
Author: User

Data type

in the ES , there are 6 basic data types :number,String,Boolean , Null,Undefined, and ES6 Newly added Symbol

Undefined Data Types

There is only one value for this data type:undefiend

when a variable is declared, but the variable is not assigned a value, the value of the variable is undefined

If a variable, even the declaration is not declared, then print this variable will be directly error, but if you use typeof to see the variable type when it will show undefined

Null Data Type

This data type has only one value, which is nullandnull means null. Undefined can be seen as being derived from null .

Boolean Data Type

Chinese is called a Boolean value. The Boolean value is only two, one is true (true), and the other is False (false). Because It is strictly case-sensitive inside ES, true and true are not the same thing.

any other data type can be converted to a Boolean type

Data type

True

False

Number

Any number other than 0 can be converted to true .

Number 0

String

Any non-empty string (including a space in quotation marks, or 0in quotation marks)

Empty string

in the ES has a function that converts other data types to Boolean types:boolean ()

The following 9 values are converted to false inside the Boolean type

"": Double quote empty string

': Single quote empty string

': empty string Template

0 and -0: Numbers 0 and -0

NaN

False

Null

Undefined

Number data type

On the number we can be divided into two major categories: One is an integer, the other is a real

Integers: Integers can also be divided into positive integers and negative integers

with respect to integers, there are different binaries. Different binary, the front need to add different special symbols.

Binary :0b

octal:0

16 binary:0x

real numbers: the so-called real numbers, which are our common decimals

There are two ways to express a real number: decimal type, exponential type

maximum value and minimum value

we can pass Min_value and max_value to see The maximum and minimum values supported in ES

in the ES has an infinite concept, and if a number reaches the Infinity of 2, then this number becomes a , in contrast to the -infinity, if a number is a negative 2 of the Time Square, then it is -infinity

NaN

NaN is all called not a number, meaning it is not a count. But the data type for This NaN is number

as an identity that identifies a data that is not a number

involve any Nan will return nan

NaN is not equal to any value, including itself

in the ES is specifically judged as a non-number function IsNaN ()

numeric conversion related functions: Number () parseint () parsefloat ()

Number (): Converts a value to numeric

if it is a Boolean, it will be converted to 0 or 1 .

If it's a number, it's a simple return.

if null, returns 0

if it is undefined, return NaN

For strings, the conversion rules are as follows:

If the string contains a pure number, it will be converted to decimal, if the string is written in octal "012", this time will only be converted to a simple , but if it is hexadecimal, will be hexadecimal to decimal, binary can also be converted

if the string is not a pure number, but contains other letters or characters, then convert to NaN

if it is an empty string, then convert to 0

parseint (): Also converts other values to integers, but One of the biggest differences between it and number is that it will try to convert the string to a numeric

However, it is important to note that although it is possible to convert to a number, it must start with a number, and if it starts with a letter or a character , it will be converted to NaN

parseint () accepts the second parameter, indicating how much of the preceding data is binary

The difference between the and number () function is that the function is to convert it to an integer

Parsefloat (): converted to decimal. As with parseint () , keep as many as you can.

in the before ES6,number(),parseint () , and parsefloat () is a method that belongs to a global object, but starting with ES6 , the above method has been categorized into the number object

Number.isinteger (): Determines whether a number is an integer, or False if it returns true

String Data Type

Strings enclose a string of characters using quotation marks. It can be either single quotation marks or double quotes. There is no difference.

if double quotes are used outside the string, the interior is enclosed in single quotes and vice versa.

If you use double quotes externally and you want to use double quotes internally, you need to add the escape character \

You can convert other data types to string types,toString (): You can add null and undefined in addition to the Data type other than to string

You can pass in a parameter, specifying how many binary

String () to convert all data types to strings.

The string data type, combined with any other data type, eventually gets a string type. So the way to quickly convert a non-string data type to a string is to add it to an empty string

ES6 adds a new one called a string template. Use two anti-quotes '

The string template has two functions, the first one is to implement multiple lines of text

The second function: Can parse the variable, only need to parse the variable into ${} inside can parse

typeof Operator: Returns the data type of the variable, mainly to pay attention to a few more special examples

type conversion: Can be divided into two categories, one is called explicit conversion, one is called implicit conversion

Implicit conversion: The so-called implicit conversion, refers to the internal system of automatic conversion. Implicit conversions typically occur when different data types are being operated on.

Explicit Conversions: Our programmers force data type conversions. The main thing is to turn the Boolean, turn the number, turn the string

Turn Boolean: Boolean ()

Turn numbers: Number () parseint () parsefloat ()

go to string:toString () string ()

Quick Method:

go to Boolean: Just add one in front of the name ! , if you want to convert to a Boolean value that matches the current type, add two !!

Number:* or /1

go to string: Add to empty string

JS 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.