Home > Web Front-end > JS Tutorial > Summary of JS data type conversion

Summary of JS data type conversion

小云云
Release: 2018-02-12 15:19:43
Original
1747 people have browsed it

There are two types of data type conversion in JS: forced type conversion and implicit type conversion. This article mainly shares with you a summary of JS data type conversion, hoping to help everyone.

Implicit type conversion principle

  1. are all numbers, direct operations

  2. are all strings, use the corresponding characters ASCII value operation

  3. One of them is a boolean value, convert the boolean value into a number, true->1 or false->0

  4. One of them is a number, convert the other one into a number

  5. If there is NaN, the result is NaN or false

Convert the reference type to Primitive type analysis

Four operating functions are defined inside JS

  • ToPrimitive(obj, [PreferredType])
    obj: The object to be converted
    PreferredType: The target type to be converted (only accepts Number and String types, the default is empty), if obj is Date, then PreferredType=String, in other cases PreferredType=Number

  • ##ToNumber(arguments)

  • ToString(arguments)

  • ##ToBoolean(arguments)

  • Here is an example of converting an object to a number


Summary of JS data type conversion to Number

Summary of JS data type conversion Convert to String

Summary of JS data type conversionRelated recommendations:

Specific analysis of data type conversion in JavaScript

Detailed explanation of data type conversion examples in php

A brief discussion on data type conversion in javascript

The above is the detailed content of Summary of JS data type conversion. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template