Soft Technology Web Classroom: JavaScript type conversions

() Converting values Number The, String () converts the string, Boolean () converts a Boolean value.

JavaScript data types

JavaScript There are five types of data values ​​may comprise:

  • String (string)
  • Digital (number)
  • Boolean (boolean)
  • Object (object)
  • Function (function)

There are three types of objects:

  • Object (Object)
  • Date (Date)
  • Array (Array)

Meanwhile, there are two types of data can not contain the value:

  • null
  • undefined

typeof operator

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

Examples

typeof "Bill"                  // returns "String" 
typeof 3.14                    // returns "Number" 
typeof NaN3                     // returns "Number" 
typeof  to false                   // returns "Boolean" 
typeof [1,2,3,4]               // Returns "object" 
typeof {name: 'Bill', Age: 62 is}   // returns "Object" 
typeof  new new a Date ()              // returns "Object" 
typeof  function () {}          // return "function"
typeof myCar                  //Return "undefined" * 
typeof  null                    // returns "object"

 

caution:

  • The data type is numerical NaN
  • Data type of the array is the object
  • Date data type is an object
  • null data type is the object
  • Data type is undefined undefined variables
  • Data type has not been assigned a variable is undefined

You can not use  typeof to determine whether the object is a JavaScript array (or date).

typeof data types

typeof operator is not a variable. It belongs to the operator. Operators (such as  /) no data types.

However, typeof always returns the string (containing the type operand).

constructor property

constructor property returns all the constructor function JavaScript variable.

Examples

"Bill".constructor                 // 返回 "function String()  { [native code] }"
(3.14).constructor                 // 返回 "function Number()  { [native code] }"
false.constructor                  // 返回 "function Boolean() { [native code] }"
[1,2,3,4].constructor              // 返回 "function Array()   { [native code] }"
{name:'Bill', age:62}.constructor  // 返回" function Object()  { [native code] }"
new Date().constructor             // 返回 "function Date()    { [native code] }"
function () {}.constructor         // 返回 "function Function(){ [native code] }"

 

You can check  to determine whether an object constructor property is an array (that contains the word "Array"):

Examples

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

 

Or, more simply, you can check whether the object is an array of functions:

Examples

function isArray(myArray) {
    return myArray.constructor === Array;
}

 

You can check  to determine whether an object constructor property to date (contains the word "Date"):

Examples

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

 

Or, more simply, you can check whether the object is a date function:

Examples

function isDate(myDate) {
    return myDate.constructor === Date;
}

 

JavaScript type conversions

JavaScript variables can be converted into new variables, and another data type:

  • Through the use of JavaScript function
  • Automatic conversion by JavaScript itself

The value is converted to a string

Global method  String () can be converted to a digital string.

It can be used in any type of numeric, text, variables or expressions:

Examples

String (X)          // X Returns a string from numerical variables 
String (123)        // return value from a text string 123 
String (+ 23 is 100)   // Returns a string from the numerical expression

 

Digital method  toString () Similarly.

Examples

x.toString()
(123).toString()
(100 + 23).toString()

 

method description
toExponential() Returns a string, rounding of numbers, and to write using exponential notation.
toFixed() Returns a string, rounding of numbers, and uses the specified number of decimal places to write.
toPrecision() Returns a string, the number written to the specified length.

The Boolean is converted to a string

Global method  String () can be converted into Boolean string.

String ( false )         // returns "false" 
String ( to true )          // returns "true"

Boolean method  toString () empathy.

false .toString ()      // returns "false" 
to true .toString ()       // returns "true"

 

To convert a string date

Global method  String () can be converted to the date string.

String (a Date ())       // returns "Fri Dec 13 2019 09:48:21 GMT + 0800 ( China Standard Time)"

Date method  toString () empathy.

Examples

DATE (). ToString ()    // returns "Fri Dec 13 2019 09:48:21 GMT + 0800 ( China Standard Time)"

 

method description
getDate() In order to obtain numerical count (1-31) of the Day
getDay() In numerical terms or periphery (0-6) of
getFullYear() Get four-digit years (yyyy)
getHours() Obtaining (0-23)
getMilliseconds() Ms is obtained (0-999)
getMinutes() Get minutes (0-59)
getMonth() Obtaining month (0-11)
getSeconds() Get seconds (0-59)
getTime() Get time (in milliseconds since January 1, 1970)

Convert numeric strings

Global method  Number () can be converted into a digital string.

String (such as "3.14") comprising a digital conversion to digital (such as 3.14).

0 is converted to an empty string.

Converted to other strings  NaN (Not a number, not a number).

Number The ( "3.14")     // Returns 3.14 
Number The ( "")        // returns 0 
Number The ( "")         // returns 0 
Number The ( "99 88")    // returns NaN

 

method description
parseFloat() Parse the string and returns a float.
parseInt() Parse the string and returns the integer.

One yuan + operator

Unary  + operator can be used to convert a digital variable:

Examples

var Y = ". 5";       // Y is a string 
var X = Y +;       // X is a number

 

If you can not convert variable, it will become digital, but the value is  NaN (Not a number):

Examples

var Y = "Bill";    // Y is a string 
var X = Y +;       // X is a number (NaN)

Boolean value to convert

Global method  Number () can also be converted to a digital Boolean.

Number The ( to false )      // Returns 0 
Number The ( to true )       // Returns 1

The conversion date for digital

Global method  Number () may be used to convert a digital date.

d = new Date();
Number The (D)           // Returns 1576201701989

 

Date method  getTime () empathy.

d = new Date();
d.getTime ()         // return 1576201701992, timestamp

 

Automatic type conversion

If JavaScript attempt to operate a kind of "wrong" data type, it will attempt to convert the value to the "right" type.

The results are not always what you expect:

5 + null     // return null because 5 is converted into 0 
"5" + null   // Returns "5null" because null is converted to "null" 
, "5" + 2      // returns 52 because 2 is converted to "2" 
. " 5 "- 2      // return 3 as" 5 "is converted into 5 
" 5 "*" 2 "    // returned as 10" 5 "and" 2 "is converted into 2 and 5

Automatic string conversion

JavaScript automatically calls the variable  toString () function, when you try to "output" object or variable:

document.getElementById("demo").innerHTML = myVar;

// If myVar =: {name "Fjohn" } // toString converted to "[Object Object]" 
// if myVar = [1,2,3,4] // toString convert "1,2,3,4 " 
// if myVar = new Date () // toString converted to" Fri Dec 13 2019 09:48:21 GMT + 0800 ( China standard time) "

 

Numbers and Boolean will be converted, but not obvious:

// If myVar = 123 // toString converted to "123" 
// if myVar = true // toString convert "to true" 
// if myVar = false // toString converted to "false"

 

JavaScript type conversion table

The following table lists the different JavaScript converted into digital values, and Boolean strings results:

Original value Converted to digital Into a string Convert logic
false 0 "false" false
true 1 "true" true
0 0 "0" false
1 1 "1" true
"0" 0 "0" true
"000" 0 "000" true
"1" 1 "1" true
NaN NaN "NaN" false
Infinity Infinity "Infinity" true
-Infinity -Infinity "-Infinity" true
"" 0 "" false
"20" 20 "20" true
"twenty" NaN "twenty" true
[ ] 0 "" true
[20] 20 "20" true
[10,20] NaN "10,20" true
["twenty"] NaN "twenty" true
["ten","twenty"] NaN "ten,twenty" true
function(){} NaN "function(){}" true
{ } NaN "[object Object]" true
null 0 "null" false
undefined NaN "undefined" false

 

Guess you like

Origin www.cnblogs.com/sysoft/p/12033535.html