Skip to content

wordCount

Counts the number of words in a string.

Syntax

typescript
wordCount(str: string): number

Parameters

ParameterTypeDescription
strstringThe input string.

Returns

TypeDescription
numberThe number of words in the string.

Example

typescript
wordCount("Exemplo de string"); // 3
wordCount("  Teste   word count  "); // 3
wordCount(""); // 0

Notes

  • Throws TypeError if the argument is not a string.
  • Words are separated by whitespace.

References

Released under the MIT License.