Count the number of words in a string
JavaScript
const countWords = (str) => str.trim().split(/\s+/).lengthTypeScript
const countWords = (str: string): number => str.trim().split(/\s+/).lengthExamples
countWords('Hello World') // 2
countWords('Hello World') // 2
countWords(' Hello World ') // 2