Home Web Front-end JS Tutorial js data type conversion summary notes_javascript skills

js data type conversion summary notes_javascript skills

May 16, 2016 pm 06:11 PM
Data type conversion

JavaScript has the following data type conversion methods:
One, convert it into a number xxx*1.0
Convert it into a string xxx ""
Two, extract one value from another A type of value and conversion is done.
. Extract the integer from the string: parseInt();
Example: the result of parseInt("123zhang") is 123
. Extract the floating point number from the string: parseFloat();
Example: The result of parseFloat("0.55zhang") is 0.55
. Execute a piece of javascript code represented by a string: eval();
Example: The result of zhang=eval("1 1") is zhang =2
. Convert to string: toString();
Example: the result of zhang=eval("1 1") zhang=2
three is to convert the entire value from one type Convert to another data type (called basic data type conversion),
Three methods of basic data type conversion:
. Convert to character type: String(); Example: String(678) The result of Number("678") is "678"
. Convert to numeric type: Number(); Example: The result of Number("678") is 678
. Convert to Boolean type: Boolean(); Example: Boolean("aaa ") is true

When using these methods, if necessary, try to judge and handle exceptions on the execution of parameters and methods.
As seen in the reference document, the following is a summary of execution efficiency:
Under IE, the first method is the fastest, the second is the second, and the third is the worst, but the difference is only 100,000 times. , the difference is only tens of hundreds of milliseconds.
Under FF, the first and second types are basically equivalent, and the third type is the slowest.
The speed difference is basically negligible. Because the difference is very small.
However, from the simplicity of the code, the first method is obviously simple to write and easy to read.
And there will be no error in the second method because an object does not have a toString method. Besides, he was always the fastest.
So, I am accustomed to using the first method to complete data type conversion.
However, if you need "123456abcd" to extract the numbers in it, then you should naturally use functions such as parsetInt and parseFloat.
But please note that sometimes the conversion result is NaN, etc., so you need to judge.

Example exception handling:

Copy code The code is as follows:

/ /Execute the statement in the text box and use eval to return the value after the statement is executed
function doFunction(str) {

var result = str.replace(new RegExp(""", "gm"), "");
//Determine whether the statement is legal
var reg = /s*[A-Za-z] s*('. ')s*$/;
if (result.match( reg)) // Or use the reg.test(result); method to determine
try {
result = eval(result);
return result;
}
catch (Error) {
alert("Sorry! The statement cannot be converted. Pay attention to grammar and spelling.");
return;
}
else alert("Please check the format and syntax of the data type conversion statement!");
}

Other test examples on data conversion methods are as follows:
例  句 结  果
parseInt('1234') 1234
parseInt('1234.00') 1234
parseInt('1234abc') 1234
parseInt('abc1234') undefined(转换失败)
parseFloat('1234.123') 1234.123
parseFloat('1234.123a') 1234.123
parseFloat('a1234.123') NaN
Number('1234.123') 1234.123
Number('1234.123aa') NaN
String(eval('12 10')) 22
Boolean('0'),Boolean('567'),Boolean(567) true
Boolean(null),Boolean(false),Boolean(0),Boolean(''),Boolean() false
this.toString() [object]
(typeof(this)).toString() object
eval('12 34') 46
eval('12 34') '' 46
eval('12 34')*1.0 46
typeof(eval('12 34')*1.0) number
typeof(eval('12 34') '') string

Note: The environment during the test is vs2008, ie8..., which is the statement entered on the page. Use eval in js to process the conversion example sentence in the left column of the above table.

You may need to modify symbols, etc. when using it.

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

How to use v-model.number to implement data type conversion of input boxes in Vue How to use v-model.number to implement data type conversion of input boxes in Vue Jun 11, 2023 am 08:54 AM

In Vue, v-model is an important instruction used to implement two-way binding. It allows us to easily synchronize user input to Vue's data attribute. But in some cases, we need to convert the data, such as converting the string type input by the user into a numeric type. In this case, we need to use the .number modifier of v-model to achieve this. Basic usage of v-model.number v-model.number is a modification of v-model

How to use CONVERT function in MySQL for data type conversion How to use CONVERT function in MySQL for data type conversion Jul 12, 2023 pm 01:57 PM

How to use the CONVERT function in MySQL to convert data types. In the MySQL database, the CONVERT function is a very practical function that can be used to convert data types. By using CONVERT function, we can convert one data type to another data type, which is very useful when dealing with data of different data types. This article will explain how to use the CONVERT function for data type conversion and provide some practical code examples. 1. Requirements for data type conversion

Research on solutions to data type conversion problems encountered in development using MongoDB technology Research on solutions to data type conversion problems encountered in development using MongoDB technology Oct 08, 2023 am 09:53 AM

Research on solutions to data type conversion problems encountered in development using MongoDB technology Abstract: When using MongoDB for data development, conversion problems between data types are often encountered. This article will explore common data type conversion problems during the development process and provide corresponding solutions. This article will combine code examples to introduce how to use MongoDB's built-in functions and operators to handle data type conversion. Introduction In the data development process, data type conversion is a common and important issue. different data

PHP8 data type conversion: methods and case sharing to improve conversion efficiency PHP8 data type conversion: methods and case sharing to improve conversion efficiency Jan 05, 2024 am 09:01 AM

PHP8 data type conversion: efficient conversion methods and case sharing Introduction: Data type conversion is a very common operation in programming, especially in scenarios such as processing user input, data storage and output. In PHP8, data type conversion operations are more efficient and flexible. This article will introduce commonly used data type conversion methods in PHP8, and demonstrate its practical application through specific code examples. Basic data type conversion 1.1 String to integer conversion In PHP8, you can use (int), intval(),

How to implement data type conversion function in TypeScript using MySQL How to implement data type conversion function in TypeScript using MySQL Jul 29, 2023 pm 02:17 PM

How to implement data type conversion function in TypeScript using MySQL Introduction: Data type conversion is a very common requirement when developing web applications. When processing data stored in a database, especially when using MySQL as the back-end database, we often need to convert the data in the query results to the type we require. This article will introduce how to use MySQL to implement data type conversion in TypeScript and provide code examples. 1. Preparation: Starting

Sharing of data type conversion methods in MySQL Sharing of data type conversion methods in MySQL Jun 15, 2023 pm 08:51 PM

MySQL is currently a widely used open source relational database. It supports a variety of data types, including integers, strings, date and time, etc. In practical applications, we often need to convert different data types to meet various needs. This article will share the data type conversion methods in MySQL, including implicit conversion and explicit conversion. 1. Implicit conversion Most data types in MySQL can be implicitly converted, that is, they are automatically converted to the appropriate type during operation. Let's demonstrate it through an example: convert date type number

Java Error: Wrong Data Type, How to Fix and Avoid Java Error: Wrong Data Type, How to Fix and Avoid Jun 25, 2023 am 09:28 AM

With the widespread use of Java, developers often encounter some common errors, one of which is "wrong data type (Typemismatch error)". This error can occur when a variable is assigned, a method parameter is passed, or a function return value does not conform to the expected data type. This article will introduce wrong data types in Java and how to solve and avoid such errors. 1. Wrong data type In Java, each variable has a specific data type, such as integer type int and floating point type.

PHP8 Data Type Conversion: Quick Guide and FAQs PHP8 Data Type Conversion: Quick Guide and FAQs Jan 05, 2024 pm 06:11 PM

PHP8 Data Type Conversion: A Concise Guide and FAQ Overview: In PHP development, we often need to convert between data types. PHP8 provides us with many convenient data type conversion methods, which can easily convert between different data types and process data effectively. This article will provide you with a concise guide and FAQ, covering commonly used data type conversion methods and sample code in PHP8. Converting strings to integers When processing user input, database queries, etc., we often need to convert characters

See all articles