Getting Name initials using JS

I would like to extract initials from a string, like:

Name = FirstName LastName 
Initials =  FL

I can get the above result using this,

const initials = item
    .FirstName
    .charAt(0)
    .toUpperCase() +
  
    item
    .LastName
    .charAt(0)
    .toUpperCase();

But now my requirements are changed as if name only consist of 1 word or more then 2, so in following cases how can I get initials as per my requirements,

FullName =  FU
FirstName MiddleName LastName = FL
1stName 2ndName 3rdName 4thName 5thName = 15

How can I get above initials from a string in JS?

Also now I only have item.Name string as an input

20 thoughts on “Getting Name initials using JS”

Leave a Comment