get initials from name javascript code example
Example 1: js get initials from name
const fullName = nameString.split(' ');
const initials = fullName.shift().charAt(0) + fullName.pop().charAt(0);
return initials.toUpperCase();
Example 2: get initials from full name javascript
const fullName1 = "Noah William";
const fullName2 = "Jackson";
const getInitials = (name) => {
let initials = name.split(' ');
if(initials.length > 1) {
initials = initials.shift().charAt(0) + initials.pop().charAt(0);
} else {
initials = name.substring(0, 2);
}
return initials.toUpperCase();
}
console.log(getInitials(fullName1))
console.log(getInitials(fullName2))