Home > Web Front-end > JS Tutorial > body text

What are the methods for javascript parameter type conversion?

coldplay.xixi
Release: 2023-01-05 16:09:45
Original
2678 people have browsed it

Javascript parameter type conversion method: 1. Explicit data type conversion, including Number conversion, other types to boolean values, etc.; 2. Implicit conversion, including conversion to number, conversion to string, etc.

What are the methods for javascript parameter type conversion?

The operating environment of this tutorial: Windows 7 system, JavaScript version 1.8.5, DELL G3 computer.

Javascript parameter type conversion method:

1, explicit data type conversion

a: Conversion Number:

1) Number conversion:

Code:

var a = “123”;
a = Number(a);
Copy after login

Note:

a) If the converted content itself is a numeric type string, then it will return itself when converting in the future.

b) If the content to be converted is not a numerical string, the result will be NaN during conversion.

c) If the content to be converted is an empty string, then The result of the conversion is 0.

d) If it is other characters, the result will be NaN.

2) parseInt():

code :

var a = “123”; a = parseInt(a);
Copy after login

a) Ignore the spaces in front of the string until the first non-empty character is found, and also remove the non-digit string after the number.

b) If the first character is not a numeric sign or a negative sign, NaN is returned

c) The decimal will be rounded. (Round 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():

Code:

var a = 123;
a = String(a);
Copy after login

2).toString() method to convert (wrapper class).

Code:

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

undefined, null cannot use toString.

c. Convert to boolean type:

You can convert other types to boolean values:

Boolean():

Code :

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

Note: When performing boolean conversion, all contents will result in true after conversion, except: false, "" (empty string), 0, NaN, undefined

2, Implicit conversion

a) Convert number:

var a = “123”;
a = +a;
Copy after login

Addition, subtraction, multiplication, division and the remainder can implicitly convert a string into a number.

b) Convert string:

var a = 123;
a = a + “”;
Copy after login

c) Convert boolean:

var a = 123;
a = !!a;
Copy after login

Related free learning recommendations: javascript video tutorial

The above is the detailed content of What are the methods for javascript parameter 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