Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

javascript find shortest word in string

const findShort = str => {
  // Convert string into an array of individual words:
  const arr = str.split(' ');
  // Sort array in ascending order:
  arr.sort((a, b) => a.length - b.length);
  // Return the first (shortest) element in the array:
  return arr[0];
};
 
PREVIOUS NEXT
Tagged: #javascript #find #shortest #word #string
ADD COMMENT
Topic
Name
1+9 =