Home  >  Article  >  Web Front-end  >  What are the methods for javascript parameter type conversion?

What are the methods for javascript parameter type conversion?

coldplay.xixi
coldplay.xixiOriginal
2021-04-19 15:44:552677browse

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

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

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

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

Code:

var a = 123; a = a.toString();

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

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;

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

b) Convert string:

var a = 123;
a = a + “”;

c) Convert boolean:

var a = 123;
a = !!a;

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!

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