0

By using parseInt(x), we can get an integer value from a string. But same functionality we can achieve from +(x) also. What exactly the functional difference are. Ex:

let input = '123';
let output = parseInt(input); // gives output as integer 123
let output = +(input); // gives output as integer 123
BALA
  • 1,170
  • 1
  • 12
  • 13

1 Answers1

1
  • The parseInt() function parses a string argument and returns an integer of the specified radix (the base in mathematical numeral systems)whereas the unary + acts more like parseFloat since it also accepts decimals.
  • The parseFloat() function parses a string and returns a floating point number.
  • Therefore if you are validating user input, unary plus has correct behavior for everything except it accepts decimals, whereas parseInt is too liberal.
  • Here the String that you entered(ie, '123') does not have decimal point. So the output will be same.
Vignesh VS
  • 921
  • 1
  • 14
  • 30