Data types and type conversion in JavaScript

angryTom
Release: 2020-02-28 18:00:50
forward
2719 people have browsed it

This article will introduce you to the data types in JavaScript and the mutual conversion of various data types. I hope it will be helpful to friends who are learning JavaScript!

Data types and type conversion in JavaScript

There are 6 different data types in JavaScript:

string (string)
number (number)
boolean (Boolean)
object (object)
function (function)
symbol (one of the data types of ES6)

There are 3 object types:

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

There are 2 data types that do not contain any values:

null
undefined

A few things to note:

The data type of NaN is number
The data type of array (Array) is object
Date The data type of (Date) is object
null’s data type is object
The data type of undefined variable is undefined

constructor attribute

## The #constructor attribute returns the constructor of all JavaScript variables.

Data types and type conversion in JavaScript

You can use the constructor property to check whether the object is an array:

Data types and type conversion in JavaScript

You can use the constructor property to check whether the object is an array Date

Data types and type conversion in JavaScript

JS type conversion

Convert number to string

Global The method String() can convert a number into a string.

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

Data types and type conversion in JavaScriptNumber method toString() can also complete the conversion.

Data types and type conversion in JavaScript

Convert Boolean value to string

Global method String() can convert Boolean value to string.


Data types and type conversion in JavaScriptBoolean method toString() can also complete the conversion.

Data types and type conversion in JavaScript

Convert date to string

Date() returns a string.


Data types and type conversion in JavaScriptGlobal method String() can convert a date object into a string.

Data types and type conversion in JavaScriptDate method toString() can also complete the conversion

Data types and type conversion in JavaScript

Convert string to number

Global method Number() can convert a string into a number.

Strings containing numbers (such as "3.14") are converted to numbers (such as 3.14).

Empty strings are converted to 0.

Other strings will be converted to NaN (not a number).

Data types and type conversion in JavaScript

Unary operator

Unary operator can be used to convert a variable into a number.

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

Data types and type conversion in JavaScript

Convert the boolean to Number

The global method Number() converts a Boolean value to a number.

Data types and type conversion in JavaScript

Convert date to number

Global method Number() can convert date to number.

Data types and type conversion in JavaScriptThe date method getTime() also has the same effect.

Data types and type conversion in JavaScript

Automatic conversion of type

When JavaScript tries to operate on a "wrong" data type, it will automatically convert to "correct" data type.

Data types and type conversion in JavaScript

Automatic conversion to string

When trying to output an object or a variable, JavaScript will automatically call the variable's toString() method.

Data types and type conversion in JavaScript
Data types and type conversion in JavaScript
Data types and type conversion in JavaScript
Similarly, when the output variables or objects are numbers and Boolean values, they will also be converted into strings

Data types and type conversion in JavaScript

Convert frequently used different values to numbers (Number), strings (String), Boolean values (Boolean):

Data types and type conversion in JavaScript

This article comes fromjs tutorialcolumn, welcome to learn!

The above is the detailed content of Data types and type conversion in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!