Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - How to convert a string into an integer with js
How to convert a string into an integer with js
There are three main methods.

Conversion function, forced type conversion, weak type conversion using js variables.

1. transfer function:

Js provides two conversion functions, parseInt () and parseFloat (). The former converts values into integers, and the latter converts values into floating-point numbers. Only when these methods are called on String type can these two functions run correctly; Other types return NaN.

2. Forced type conversion

You can also use type conversion to handle the types of converted values. Using a cast allows you to access a specific value, even if it is another type.

The three types of casts available in ECMAScript are as follows:

Boolean(value)- Converts a given value to a Boolean type;

Number(value)- Converts a given value into a number;

String(value)- Converts the given value into a string.

Converting a value using one of these three functions will create a new value and store the value directly converted from the original value. This will have unexpected consequences. When the value to be converted is a string containing at least one character, a non-zero number or an object, the Boolean () function will return true. If the value is an empty string, the number 0, undefined or null, it will return false.

3. Weak type conversion using js variables