Create slug in Javascript

Creating slug is so simple in Javascript. Slug is very useful specially for creating using out of it. Using javascript's built-in functions we can create slug in javascript. First thing we can do it to trim the string and then a toLowerCase function with a small regular express like below:
function generateSlug(item){
  return item.trim().toLowerCase().replace(/ +/g,'-');
}

Call and output

console.log(generateSlug('ff ad add '));
ff-ad-add // output

Comments