Browse - Programming Tips - javaScript: compact way to convert a string to an integer

Date: 2019may27
Language: javaScript

Q.  javaScript: compact way to convert a string to an integer

A.  You can use parseInt()

const parsed = parseInt(x, base); const parsed = parseInt('123', 10);
Or unary +
const n = + '123';
Its more compact but not as clear to readers. Probably best used by minifiers. In my tests, unary + assume base 10.