rehipex378
rehipex378

Reputation: 71

Split Full Name to First Name and Last Name in Javascript

I have this var fullName = 'Muhammad Ali', what I want to do is split the First Name and Last name from it.

Upvotes: 0

Views: 1138

Answers (3)

Mattia Rasulo
Mattia Rasulo

Reputation: 1451

Using a library like parse-full-name would usually get you a better result than using a simple split on strings.

The string may have titles inside or other inconsistencies that a library would hopefully parse better.

Upvotes: 0

Sean
Sean

Reputation: 1456

You can use the .split() method to split the string to an array based on spaces in the string. I would use caution with this as names can differ in length and have more than one space.

Upvotes: 0

Scollier
Scollier

Reputation: 631

For simple cases, use .split(). Usually a simple google search can help you.

let fullName = 'Muhammad Ali'
let formattedName = fullName.split(" ")
console.log(formattedName)

Upvotes: 3

Related Questions