This is the 13th day of my participation in the August More Text Challenge.More challenges in August

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

There are six different data types in JavaScript:

  • string
  • number
  • boolean
  • object
  • function
  • symbol

There are three object types:

  • Object
  • Date
  • Array

Two data types that do not contain any values:

  • null
  • undefined

The typeof operator

You can use the Typeof operator to see the data types of JavaScript variables.

typeof "hhhh"                 / / return a string \
typeof 5.20                   / / return number \
typeof NaN                    / / return number \
typeof true                  / / return Boolean \
typeof [1.2.3]              / / returns object \
typeof {name:'hhh'.age:14}  / / returns object \
typeof new Date(a)/ / returns object \
typeof function () {}         / / return the function \
typeof myCar                  // return undefined (if myCar is not declared)\
typeof null                   / / return the object
Copy the code
  • The data type for NaN is number
  • The data type of an Array is Object
  • The data type of a Date is Object
  • The data type of NULL is Object
  • The data type of an undefined variable is undefined

If the object is a JavaScript Array or JavaScript Date, there is no way to determine their type from typeof, since both return Object.

JavaScript type conversion

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

  • By using JavaScript functions
  • Automatically converts through JavaScript itself

Converts numbers to strings

The global method String() converts numbers to strings.

This method can be used with any type of numbers, letters, variables, expressions:

String(z)         // Convert the variable z to a string and return \
String(3445566)       // Convert the number 3445566 to a string and return \
String(575475 + 85654)  // Converts a numeric expression to a string and returns it
Copy the code

Number method toString ()

z.toString()\
(3485).toString()\
(45678 + 3357).toString()
Copy the code

Converts a Boolean value to a string

The global method String() converts a Boolean value to a String.

String(false)        / / return "false"
String(true)         / / return "true"
Copy the code

The Boolean method toString() has the same effect.

false.toString()     / / return "false" \
true.toString()      / / return "true"
Copy the code

Converts the date to a string

Date() returns a string.

Date(a)// Return "Fri Aug 13 2021 23:04:48 GMT+0800"
Copy the code

The global method String() converts a date object to a String.

String(new Date())      // Return "Fri Aug 13 2021 23:05:07 GMT+0800"
Copy the code