Filter a list element starting with a letter in JavaScript

const freinds = ['Salman', 'Asad',  'Shahrukh', 'Aman'];
const startsS = freinds.filter((freind) => freind.startsWith("s"));
console.log(startsS);

Output:

[ 'Salman', 'Shahrukh' ]


This is roughly the same as @adeneo's answer, just a little terser:

const countries = ['Norway', 'Sweden',  'Denmark', 'New Zealand'];

const startsWithN = countries.filter((country) => country.startsWith("N"));

console.log(startsWithN);

// Output: [ 'Norway', 'New Zealand' ]

You'd filter based on a condition, the named function returns true or false based on that condition

const countries = ['Norway', 'Sweden', 'Denmark', 'New Zealand'];

function firstN(item) {
  return item.toLowerCase().indexOf('n') === 0;
}

let startsWithN = countries.filter(firstN);

console.log(startsWithN)