Skip to content

countWords ​

Counts the number of words in a string.

Parameters ​

  • str (string): The input string.

Returns ​

  • number: The number of words in the string.

Usage ​

ts
countWords('Hello world'); // 2
countWords('   This is a test.   '); // 4
countWords(''); // 0