// Input sentence
const sentence = 'This is a sample sentence.';
// Calculate word count by splitting the sentence at spaces
const wordCount = sentence.split(' ').length;
// Display the word count
console.log('Word Count:', wordCount);
code
JavaScript Word Count in a Sentence
This JavaScript code calculates the word count in a given sentence by using the split method to separate the words based on spaces and then determining the length of the resulting array. The comments provide explanations for each part of the code.
String
Word Count
split Method