Detailed explanation of JavaScript data type conversion rules _javascript tips

Source: Internet
Author: User
Tags numeric value

One, data type

5 Basic types of data:null/undefined/string/boolean/number

1 kinds of complex data types:Object

Second, data type detection

A brief summary of several ways of detecting data types in JS and its advantages and disadvantages

1, typeof

2, Instanceof/constructor

3, Object.prototype.toString.call (value)

4, Object.prototype.toString

Third, data type conversion

JS internally provides an automatic conversion mechanism for different data types, which is automatically converted to the expected type when it is expected to be a certain type rather than a type, which is what we often call implicit conversions.

1. Force type Conversion

Before you understand the rules for implicit conversions, let's take a look at coercion type conversions, which is primarily a Boolean ()/string ()/number () that converts data of various types to Boolean, string, and numeric data.

Boolean () function

The Boolean () function returns True when the value to be converted is a string of at least one character, a number other than 0 digits, or an object. If the value is an empty string, number 0, undefined, or null, it returns FALSE.

var B1 = Boolean (""); False-empty string
var b2 = boolean ("Hello");//true-Non-empty string
var B1 = boolean;//true-non 0 number
var B1 = Boolean ( NULL); False-null
var B1 = boolean (0);//false-0
var B1 = Boolean (New object ());//true-Object

Number () function

The force type conversion of the number () function is similar to the parseint () and parsefloat () methods, except that it converts the entire value, not the partial value.

The parseint () and parsefloat () methods convert only the strings before the first invalid character, so "1.2.3" is converted to "1" and "1.2" respectively.

Force type conversion with number (), and "1.2.3" returns NaN, because the entire string value cannot be converted to numbers. If the string value can be completely converted, number () will determine whether to call the parseint () method or the Parsefloat () method.

String () function

The last force type conversion method string () is the simplest because it converts any value to a string.

To perform this coercion type conversion, you only need to call the ToString () method of the value passed in as a parameter, that is, convert 12 to "12", convert True to "true", convert false to "false", and so on.

The only difference between casting to a string and invoking the ToString () method is to force a type conversion on null and undefined values to generate a string without raising an error:

var S1 = String (null); "Null"
var onull = null;
var s2 = onull.tostring (); can raise an error

2. Automatic type conversion

I'm done. Coercion type conversion, to take a look at automatic type conversions, in fact, automatic type conversions are based on coercion type conversions, and when you expect a location to be a type of data (Boolean, numeric, string), the corresponding coercion type conversion function is invoked, which is automatic.

* When JavaScript encounters an expected Boolean value (such as the conditional part of an if statement), the parameter of the non-Boolean value is automatically converted to a Boolean value. Boolean functions are automatically called within the system.

Therefore, all but the following six values are automatically converted to true.

    • Undefined
    • Null
    • -0
    • 0 or +0
    • NaN
    • ' (empty string)

* When JavaScript encounters an expected string, the data that is not string is automatically converted to a string. The string function is automatically called within the system.

The automatic conversion of strings, which occurs mainly in the addition operation. When a value is a string and another value is not a string, the latter is converted to a string.

* When JavaScript encounters an expected numeric value, the parameter values are automatically converted to numeric values. The number function is automatically called within the system.

In addition to the addition operator is likely to convert the operator to a string, the other operators will automatically convert the operator to numeric values.

The unary operator also turns the operator into a numeric value.

+ ' abc '//Nan
-' abc '//nan
+true//1
-false//0

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring some help, but also hope that a lot of support cloud Habitat community!

Related Article

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.