JavaScript type conversion

Source: Internet
Author: User
Tags object object

Number () is converted to numbers, string () to a string, and Boolean () to a Boolean value,

JS in five different data types: string,number,boolean,object,function,

Three types of objects: Object,data,array

Two types of data that do not contain any values: null undefined

typeof operator:

You can use TypeOf to view the data types of JavaScript variables:

typeof "John"                 //Return string typeof 3.14                   //return numbertypeof NaN                    //return numbertypeof false                  //return booleantypeof [1,2,3,4]              Return objecttypeof {name: ' John ', age:34}  //Return objecttypeof new Date ()             //return objecttypeof function () {}         //Return Functiontypeof MyCar                  //return undefined (if MyCar not declared) typeof null                   //Return object

< Span class= "highcom" >< Span class= "highcom" >< Span class= "Highval" >< Span class= "Highele" >< Span class= "highcom" >< Span class= "highcom" > Note:

< Span class= "highcom" >< Span class= "highcom" >< Span class= "Highval" >< Span class= "Highele" >< Span class= "highcom" >< The data type of span class= "highcom" >nan is number

< Span class= "highcom" >< Span class= "highcom" >< Span class= "Highval" >< Span class= "Highele" >< Span class= "highcom" >< The data type of the span class= "highcom" >array is Object

< Span class= "highcom" >< Span class= "highcom" >< Span class= "Highval" >< Span class= "Highele" >< Span class= "highcom" >< The data type of the span class= "highcom" >date is Object

< Span class= "highcom" >< Span class= "highcom" >< Span class= "Highval" >< Span class= "Highele" >< Span class= "highcom" >< The data type of the span class= "highcom" >null is Object

The data type of the undefined variable is undefined

< Span class= "highcom" >< Span class= "highcom" >< Span class= "Highval" >< Span class= "Highele" >< Span class= "highcom" >< Span class= "highcom" >constructor properties       Return constructors for all JavaScript variables

"John". Constructor//returns the function String () {[native code]}(3.14). Constructor//return function number () {[native code]}false. constructor//return function Boolean () {[native code]}[1,2,3,4].constructor//returns the function Array () {[native code]}{name: ' John ', age:34}.constructor//return function Object () {[native code]}NewDate (). Constructor//return function Date () {[native code]}function() {}.constructor//return function functions () {[native code]}

You can use the constructor property to see if an object is an array (containing the string "Array")

function IsArray (myArray) {    return myArray.constructor.toString (). IndexOf ("Array") >-1;}

You can use the constructor property to see if the object is a date (containing the string "date")

function isDate (mydate) {    return myDate.constructor.toString (). IndexOf ("Date") >-1;}

Javasctipt Type Conversions

1. Digital------> Strings:

The global Method string () can convert a number to a string.

String (x)         // converts the variable x to a string and returns a string (123)       // Converts the number 123 to a string and returns a string ( + +)  //  Converts a numeric expression to a string and returns

The number method toString () also has the same effect.

x.tostring ()(123). ToString () (+ +). ToString ()

2. Boolean-----> string:

The global Method string () can convert a Boolean value to a string.

String (false)        //  return "false"string (true)         //  return "true"

The Boolean Method toString () also has the same effect.

false. ToString ()     //  returns "false"true. ToString ()      //  Returns "true"

3. Date-----> string:

The global Method string () can convert a Boolean value to a string.

String (Date ())      //  return Thu Jul 15:38:19 gmt+0200 (W. Europe Daylight Time)

The Date Method toString () also has the same effect.

Date (). toString ()   //  return Thu 15:38:19 gmt+0200 (W. Europe Daylight Time)

Supplemental: Date-related methods:

Method Description
GetDate () Returns the day of the one month (1 ~ 31) from the Date object.
GetDay () Returns the day of the week from a Date object (0 ~ 6).
getFullYear () Returns the year as a four-digit number from a Date object.
GetHours () Returns the hour (0 ~ 23) of the Date object.
Getmilliseconds () Returns the milliseconds (0 ~ 999) of the Date object.
Getminutes () Returns the minute (0 ~ 59) of the Date object.
GetMonth () Returns the month (0 ~ 11) from the Date object.
Getseconds () Returns the number of seconds (0 ~ 59) of the Date object.
GetTime () Returns the number of milliseconds since January 1, 1970.

4. String----> Numbers:

The global Method number () converts the string to numbers.

strings that contain numbers (such as "3.14") are converted to numbers (such as 3.14).

The empty string is converted to 0.

The other strings are converted to NaN (not a number).

Number ("3.14")/    /  return 3.14number ("")       //number ("")/        /  Returns 0number ("the")   //  return NaN

Add: Number method:

  

Parsefloat () Parses a string and returns a floating-point number.
parseint () Parses a string and returns an integer.
Unary operator +
var y = "5";      // y is a string var x = + y;      // x is a number

If the variable cannot be converted, it will still be a number, but the value is NaN (not a number):

var y = "John";   // y is a string var x = + y;      // x is a number (NaN)

5. Boolean--------> Numbers

The global Method number () converts a Boolean value to a digit.

Number (false)     //  return 0number (true)      //  return 1

6. Date---------> numbers

The global Method number () converts a Boolean value to a digit.

D = new Date ();
Number (d) //return 1404568027739

The Date method GetTime () also has the same effect.

New Date ();d. GetTime ()         // back to 1404568027739

7. Automatic conversion type:

When JavaScript attempts to manipulate an "error" data type, it is automatically converted to the "correct" data type.

NULL    // returns 5         null cast to 0null  // returns "5null"   null converted to "null""5" + 1     // return "Wuyi"      1 to "1  ""5"-1     //  return 4         "5" to 5

8. Automatically convert to a string

When you try to output an object or a variable, JavaScript automatically calls the ToString () method of the variable:

document.getElementById ("Demo"). InnerHTML = MyVar; // if MyVar = {name: "Fjohn"}  //toString is converted to "[Object Object]"//  if MyVar = [1,2,3,4]       //TOSTR ing converts to "1,2,3,4"//  If MyVar = new Date ()      //ToString to "Fri Jul 09:08:55 gmt+0200"

Numbers and Booleans are also often converted to each other:

// if MyVar = 123             //ToString is converted to "123"//  If MyVar = True            //ToString is converted to "true" c16>//  If MyVar = False           //toString is converted to "false"

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.