Detailed explanation of js data type conversion issues
##--> 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
var a="abc"; a=Number(a); console.log(typeof a); // NaN
var a=" "; a=Number(a); console.log(typeof a); // 0
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 conversionvar a="123"; a=parseInt(a); console.log(typeof a); //number
var a=" 456467abasb"; a=parseInt(a); console.log(a); //456467
var a=" a123"; a=parseInt(a); console.log(a); //NaN
var a=123.12a=parseInt(a); console.log(a); //123
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 stringYou can convert other data types into strings
(1) String()var a123;
a=String(a);
var a=123; a=a.toString();
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);
Note: Converting , all content will result in true after conversion, except: false, " " (empty string), 0, NaN, null, undefined, ""
##2. Implicit conversiona) Convert numbervar a="123";
a=+a;
b) to String
var a=123; a=a+" ";
a=123=!!a; console.log(typeof a); //true a=!a; console.log(typeof a); //false
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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.

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

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

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

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

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 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 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
