JavaScript type conversion

Source: Internet
Author: User

JavaScript can assign values to variables at any time (numbers, strings, and so on), but what happens if I add two variables, one is a number and the other is a string?

JavaScript is smart and it will try to convert the type according to your needs. For example, if you add a string and a number, it converts the number to a string and then concatenates the two strings together. This is the JavaScript type conversion that this article is going to talk about,

Introduced:

The variables in JavaScript can be assigned to reference value or primitive value. Reference value, also known as an object, is stored in the heap, and the variable Reference value points to the address of the object in memory. Primitive value contains undefined, Null, Boolean, number, String. Reference value includes function, array, regex, date, and so on.

We can use TypeOf to determine the data type of the JavaScript variable.

typeof NULL;//"Object"typeofUndefined//"undefined"typeof0;//"Number"typeofNaN;//"Number"typeof true;//"Boolean"typeof"Foo";//"string"typeof{};//"Object"typeof function(){};//"function"typeof[]; "Object"

1. Convert to Boolean

You can cast any type of variable to a Boolean value by using the Boolean () function. There is also the comparison operation of a = = (> or <) b when JavaScript encounters a place that is expected to be a Boolean value (such as the condition part of the IF statement, a &&b (a| | B or!a), the non-Boolean parameter is automatically converted to a Boolean value.

Primitive value is converted to true except,,,,, and undefined null convert to ""(空字符串) 0 -0 NaN false.

Value Undefined Null True False "" (empty string) "1.2" "One" 0 -0 NaN Infinity -infinity 1 (Other non-infinity numbers) {} [] [9] [' A '] function () {}
Boolean value False False True True False True True False False False True True True True True True
True True

Boolean (undefined);//falseBoolean (NULL);//falseBoolean ("");//falseBoolean ("1.2")//trueBoolean (0)//falseBoolean (-0)//falseBoolean (NaN)//falseBoolean (Infinity)//trueBoolean (-infinity)//trueBoolean (1)//trueBoolean ({})//trueBoolean ([])//trueBoolean ([9])//trueBoolean (' a ')//trueBoolean (function(){})//true

Any Boolean value of reference value (object) is true. Even a Boolean object that has a value of false, undefined, or null has a value of true.

Boolean (new boolean (undefined))   //Trueboolean (new Boolean (null) )   //Trueboolean (new Boolean (false))   //true

In a conditional statement, these Boolean objects are judged as true. For example, in the following conditional statement, if will treat object X as true:

var New Boolean (false); if (x) {    //  ... The code here will still be executed }

2. Convert to Number

Use the number () function

Notes
    • Strings is converted to a number by stripping the leading whitespace in the string before the number and ignoring WHITESP Ace after the number. If the string does not match this pattern, then the string is converted to NaN.
    • Boolean true is converted to 1. False is converted to 0.
    • A node-set is first converted to a, string as if by a call to the string () function and then converted in the same as a String argument.
    • An object of a type and other than the four basic types are converted to a number in a, the is dependent on that type.

3. Convert to String

Reference article:

Nanyi: Data type conversions

Mdn:boolean

JavaScript type conversion

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.