JS Data type Conversion

Source: Internet
Author: User

JavaScript is a weakly typed language that does not enforce its data type when declaring a variable, and automatically transforms the data type according to the context environment.

1) String value converted to numeric

The number function parseint function (can accept the second argument, specify the binary of the conversion, the default decimal) parsefloat function

var a = number (' 100 ');
alert (a);//100
Alert (typeof (a));//number
var B = parseint (' 100 ');
alert (b);//100
Alert (typeof (b));//number

The above two examples all replace the string value with a value

Implicit conversions of string conversions to numeric values

Alert (typeof (' 1 '));//number
Alert (typeof (' + '));//number

In the subtraction operator, the string value is converted to the value by default

When it comes to addition

var str = ' abc ' + 1;
alert (str); ' ABC1 '
Alert (typeof (str));//string

Converts a value into a string value, a connection to a string

2) value converted to string

Call the string function or the ToString method

var a = String (100);
Alert (typeof (a));//string
var B = (+). toString ();
Alert (typeof (b));//string

Implicit conversions of numeric conversions to strings

When it comes to addition

var str = ' abc ' + 1;
alert (str); ' ABC1 '
Alert (typeof (str));//string

Customary methods

Convert string value to numeric

var a = ' 1 ';
Alert (typeof (+a));//number

numeric conversions to String values

var b = 1 + ";
Alert (typeof (b));//string

3) Convert other types to Boolean types

Value 0 value nan null value undefined value null string ' This 5 value will be converted to false when implicit conversion other values are converted to true

if (0) {

}

We can use!! For implicit data-type conversions

! is a Boolean operation of the logical non-operator, you can convert the operand to a Boolean type if it is not a Boolean, so pass!! You can complete the conversion of the Boolean type

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.