This will handle all of the cases and is as efficient as possible. (You don't want split(' ') unless you know beforehand that there are no spaces of greater length than one.):
var quote = `Of all the talents bestowed upon men,
none is so precious as the gift of oratory.
He who enjoys it wields a power more durable than that of a great king.
He is an independent force in the world.
Abandoned by his party, betrayed by his friends, stripped of his offices,
whoever can command this power is still formidable.`;
function wordCount(text = '') {
const words = text.trim().split(/\s+/g);
if (!words[0].length) {
return 0;
}
return words.length;
};
console.log(WordCount(quote));//59
console.log(WordCount('f'));//1
console.log(WordCount(' f '));//1
console.log(WordCount(' '));//0