'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.



Solution 1:[1]

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

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

Solution 2:[2]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Scollier
Solution 2 Sean Lawton