Home Web Front-end JS Tutorial Detailed explanation of js data type conversion issues

Detailed explanation of js data type conversion issues

Mar 26, 2017 pm 04:55 PM

##data type of js

##--> Basic types (value types): Number, Stringstring, Boolean

##-->Composite type (Reference type): Object(Array, Time type Date, FunctionType Function, Regular expressionRegExp...)

-->Empty type: Null、Undefined##1: Data type The conversion of

1 shows the conversion of

##A. Convert numbers:

If you want to convert a string type data into a number, you can use:

(1) Number conversion:

var a="123";
a=Number(a);
console.log(typeof a);    // number
Copy after login
var a="abc";
a=Number(a);
console.log(typeof a);     // NaN
Copy after login
var a=" ";
a=Number(a);
console.log(typeof a);    // 0
Copy after login

Note: ①If the converted content itself is a numeric type string, then its own number type will be returned during future conversion (special case: true returns 1 and false returns 0)

② If the converted content itself is not a numerical string, the result during conversion will be NaN

③ If the converted content is an empty string (null), then the conversion result will be 0

④If it is another string, the future conversion result will be NaN

 (2) ParseInt conversion

var a="123";
a=parseInt(a);
console.log(typeof a);    //number
Copy after login
var a="    456467abasb";
a=parseInt(a);
console.log(a);    //456467
Copy after login
var a="   a123";
a=parseInt(a);
console.log(a);     //NaN
Copy after login
var a=123.12a=parseInt(a);
console.log(a);    //123
Copy after login

Note

: ①Ignore the beginning of the string spaces, until the first non-empty character is found, the non-numeric string after the number will also be removed

② If the first number is not a numeric symbol or a negative sign, NaN

## will be returned # ③ Decimals will be rounded (rounded down) (3)parseFloat Floating point number (decimal)

Same as parseInt, the only difference is that parseFloat can retain decimals

B. Convert to string

You can convert other data types into strings

(1) String()

var a123;
a=String(a);
Copy after login
(2 )toString() method to convert (packaging class)

var a=123;
a=a.toString();
Copy after login

Note

: null and undefined do not have toString method, all types of String can be converted

C. Convert to Boolean typeYou can convert other types into boolean values ​​Boolean()

var a="true";
a=Boolean(a);
Copy after login

Note: Converting , all content will result in true after conversion, except: false, " " (empty string), 0, NaN, null, undefined, ""

##2. Implicit conversion

a) Convert number

var a="123";
a=+a;
Copy after login
Note

: +, -, *, /, % are all Strings can be implicitly converted to number

b) to String

var a=123;
a=a+" ";
Copy after login

c) to boolean

 a=123=!!a;
console.log(typeof a);    //true
a=!a; 
console.log(typeof a);    //false
Copy after login

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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Practical tips for converting full-width English letters into half-width form Practical tips for converting full-width English letters into half-width form Mar 26, 2024 am 09:54 AM

Practical tips for converting full-width English letters into half-width forms. In modern life, we often come into contact with English letters, and we often need to input English letters when using computers, mobile phones and other devices. However, sometimes we encounter full-width English letters, and we need to use the half-width form. So, how to convert full-width English letters to half-width form? Here are some practical tips for you. First of all, full-width English letters and numbers refer to characters that occupy a full-width position in the input method, while half-width English letters and numbers occupy a full-width position.

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages ​​and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

What data type should be used for gender field in MySQL database? What data type should be used for gender field in MySQL database? Mar 14, 2024 pm 01:21 PM

In a MySQL database, gender fields can usually be stored using the ENUM type. ENUM is an enumeration type that allows us to select one as the value of a field from a set of predefined values. ENUM is a good choice when representing a fixed and limited option like gender. Let's look at a specific code example: Suppose we have a table called "users" that contains user information, including gender. Now we want to create a field for gender, we can design the table structure like this: CRE

Detailed explanation of the implementation method of converting PHP months to English months Detailed explanation of the implementation method of converting PHP months to English months Mar 21, 2024 pm 06:45 PM

This article will introduce in detail how to convert months in PHP to English months, and give specific code examples. In PHP development, sometimes we need to convert digital months to English months, which is very practical in some date processing or data display scenarios. The implementation principles, specific code examples and precautions will be explained in detail below. 1. Implementation principle In PHP, you can convert digital months into English months by using the DateTime class and format method. Date

What is the best data type for gender fields in MySQL? What is the best data type for gender fields in MySQL? Mar 15, 2024 am 10:24 AM

In MySQL, the most suitable data type for gender fields is the ENUM enumeration type. The ENUM enumeration type is a data type that allows the definition of a set of possible values. The gender field is suitable for using the ENUM type because gender usually only has two values, namely male and female. Next, I will use specific code examples to show how to create a gender field in MySQL and use the ENUM enumeration type to store gender information. The following are the steps: First, create a table named users in MySQL, including

How to convert qq music to mp3 format Convert qq music to mp3 format on mobile phone How to convert qq music to mp3 format Convert qq music to mp3 format on mobile phone Mar 21, 2024 pm 01:21 PM

QQ Music allows everyone to enjoy watching movies and relieve boredom. You can use this software every day to easily satisfy your needs. A large number of high-quality songs are available for everyone to listen to. You can also download and save them. The next time you listen to them, you don’t need an Internet connection. The songs downloaded here are not in MP3 format and cannot be used on other platforms. After the membership songs expire, there is no way to listen to them again. Therefore, many friends want to convert the songs into MP3 format. Here, the editor explains You provide methods so that everyone can use them! 1. Open QQ Music on your computer, click the [Main Menu] button in the upper right corner, click [Audio Transcoding], select the [Add Song] option, and add the songs that need to be converted; 2. After adding the songs, click to select Convert to [mp3]

How to convert full-width English letters into half-width letters How to convert full-width English letters into half-width letters Mar 25, 2024 pm 02:45 PM

How to convert full-width English letters into half-width letters In daily life and work, sometimes we encounter situations where we need to convert full-width English letters into half-width letters, such as when entering computer passwords, editing documents, or designing layouts. Full-width English letters and numbers refer to characters with the same width as Chinese characters, while half-width English letters refer to characters with a narrower width. In actual operation, we need to master some simple methods to convert full-width English letters into half-width letters so that we can process text and numbers more conveniently. 1. Full-width English letters and half-width English letters

PHP Tutorial: How to convert int type to string PHP Tutorial: How to convert int type to string Mar 27, 2024 pm 06:03 PM

PHP Tutorial: How to Convert Int Type to String In PHP, converting integer data to string is a common operation. This tutorial will introduce how to use PHP's built-in functions to convert the int type to a string, while providing specific code examples. Use cast: In PHP, you can use cast to convert integer data into a string. This method is very simple. You only need to add (string) before the integer data to convert it into a string. Below is a simple sample code

See all articles