JS Data type Conversion

Source: Internet
Author: User

let's start with a brief look at the data types in JS, which have strings, numbers, booleans, arrays, objects, Null, undefiend 6 data types. At the same time, JavaScript has a dynamic type.

This means that the same variable can be used in different types:




Next, let's introduce several types of conversion of data types in JavaScript.

1. Conversion functions
JavaScript provides two conversion functions for parseint () and parsefloat (). where parseint () converts the value to an integer, parsefloat () converts the value to a floating-point number, and the parameters of the two functions can only be of type string.
Before inferring whether a string is a numeric value. parseint () and parsefloat () will parse the string in detail.

The parseint () method first looks at the character at position 0. Infer whether it is a valid number, assuming that it is not, the method returns Nan. No further operations will continue to run. But suppose the character is a valid number. This method will look at the character at position 1. Perform the same test.

This process continues until a character is found that is not a valid number. At this point parseint () converts the string before the character to a number.



The parseint () method also has a base mode that converts binary, octal, hexadecimal, or any other string into integers, regardless of the binary. The base is specified by the second parameter of the parseint () method.


2. Forcing type conversions
In JavaScript, we are also able to use coercion type conversions to handle different types of variables.

Boolean (value)--converts the given value to a Boolean type

Number (value)--converts the given value to a digit

String (value)--converts the given value to a string



Today we have a brief introduction to the data types and data type conversions in JavaScript. Just for everyone to study, JavaScript has a lot of knowledge we need to learn, we have to continue to study hard.





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