DEV Community

Cover image for Convert an array of strings to numbers
syedsimanta03
syedsimanta03

Posted on

5 1

Convert an array of strings to numbers

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

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

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

// [2, 3, 4]

Top comments (2)

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

we can use ts or input validation when writing any function

Neon image

Next.js applications: Set up a Neon project in seconds

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Get started →

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay