DEV Community

loading...
Cover image for Convert an array of strings to numbers

Convert an array of strings to numbers

syedsimanta03
UI designer | Front end developer | Graphic designer #OpenToWork » Tools: GIT, VS Code, Adobe XD, Figma, Photoshop, Illustrator
・1 min read

const toNumbers = arr => arr.map(Number);

// Or
const toNumbers = arr => arr.map(x => +x);

// Example
toNumbers(['2', '3', '4']);

// [2, 3, 4]

Discussion (5)

Collapse
itachiuchiha profile image
Itachi Uchiha

You'll get NaN in following scenarios:

Empty string -> ''

String with space ' '

function definition => () => {}

undefined value => undefined

null value => null

Collapse
syedsimanta03 profile image
syedsimanta03 Author

we can use ts or input validation when writing any function