我想写一个 return 名字和名字的第一个索引的函数,我可以使用 slice 但我希望代码自动运行,JS

i want to write a function that return first name and first index of second name, i can use the slice but i want the code works automaticly, JS

function returnName(Aname) {
  return // first name and only first charctars of seconed name
}

console.log(returnName(`Ali Ahmad`)); // Ali A 

您可以使用:

function returnName(name) {
  const [f,s] = name.split(' ')
  return `${f} ${s.charAt(0)}`
}

console.log(returnName(`Ali Ahmad`));