A parseInt() method used to convert string to number which allows space in the string. It allows space but string must starts with either space or number.
value : An integer value string that may contain space or characters
Valid conversion
1
2
3 let res1 = parseInt("100");
4 console.log(res1);
5
6
7 let res2 = parseInt(" 120 ");
8 console.log(res2);
9
10
11 let res3 = parseInt("140 number ");
12 console.log(res3);
In the above examples, a parseInt() function is called by passing a number in string data type and with leading and trailing white space that converts number values. If specified value ends with string number which is also a valid number. It converts a number and assigned to the variable and print.
Invalid conversion
1
2
3
4 let res1 = parseInt("number 100");
5 console.log(res1);
6
7
8 let res2 = parseInt("");
9 console.log(res2);
In the above example, a parseInt() is called by passing a string that starts with string number and number value and calls constructor with empty string which will return NAN that assigned to the variable and print.
Related options for your search