JavaScript Basics (18) Type conversions

Source: Internet
Author: User
Tags javascript array

JavascriptType Conversions

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

JavaScript Data types

There are 5 different types of data in JavaScript:

    • String
    • Number
    • Boolean
    • Object
    • function

3 Types of objects:

    • Object
    • Date
    • Array

2 data types that do not contain any values:

    • Null
    • Undefined
typeof operator

You can use the typeof operator to view the data type of a JavaScript variable.

typeof"John"//returns a stringtypeof3.14//return numbertypeofNaN//return numbertypeof false                  //returns a Booleantypeof[1,2,3,4]//return Objecttypeof{name: ' John ', age:34}//return Objecttypeof NewDate ()//return Objecttypeof function() {}//return functiontypeofMyCar//returns undefined (if MyCar is not declared)typeof NULL                   //return Object

Please note:

    • The data type of NaN is number
    • The data type of the array is object
    • Date data type is Object
    • The data type of NULL is Object
    • The data type of the undefined variable is undefined

If the object is a JavaScript Array or JavaScript Date, we cannot judge their type by typeof because it returns object.

Constructor property

The constructor property returns the constructor 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;}
JavaScript type Conversion

JavaScript variables can be converted to new variables or other data types:

    • By using JavaScript functions
    • Automatic conversion via JavaScript itself
Convert a number to a string

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

This method can be used for any type of number, letter, variable, expression:

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 (100 + )  //  Converts a numeric expression to a string and returns

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

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

In number, you can find more ways to convert numbers to strings:

Method Description
Toexponential () Converts the value of an object to an exponential count method.
ToFixed () Converts a number to a string, the result of which has a specified number of digits after the decimal point.
Toprecision () Formats the number as the specified length.
Convert a Boolean value to a 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"
Convert a date to a string

Date () returns a string.

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

The global method string () can convert a Date object to a string.

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

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

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

In the date method, you can see more about the functions of date conversions to strings:

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.

JavaScript Basics (18) Type conversions

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.