Counting Words
Count the pieces.
Counting Words is a free Javascript for Kids lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Javascript for Kids learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Let's Count Words! 🔢
Welcome back, word wizard! 📝 Last time we split sentences into word pieces. ✂️
Today we will count how many words there are! 🔢 Ready? Let's go!
Back to Splitting ✂️
First, let's split a sentence again to get our list of words. 📋
Remember, split(' ') gives us an array! 🪄
let sentence = 'I love my dog';
let words = sentence.split(' ');
console.log(words);How Long is the List? 📏
Every array knows its own size! We ask with .length. 📏
It tells us how many pieces are in the list. That is our word count! 🔢
let words = ['I', 'love', 'my', 'dog'];
console.log('Number of words: ' + words.length);Count Any Sentence 🎯
Now let's split AND count together! 🎯
Split the sentence, then use .length to count. Two steps, one answer! 💡
let sentence = 'coding is super fun';
let words = sentence.split(' ');
console.log('This sentence has ' + words.length + ' words! 🎉');Try a Short One 🐝
Let's try a tiny sentence! 🐝
Even small sentences get counted correctly. The computer never miscounts! 🤖
let sentence = 'hello world';
let words = sentence.split(' ');
console.log('Words: ' + words.length);Try a Long One 🦕
Now a really long sentence! 🦕
No matter how long, .length counts every single word! 💪
let sentence = 'the big brown bear ran up the tall green hill';
let words = sentence.split(' ');
console.log('This long sentence has ' + words.length + ' words!');Make a Counter Machine 🛠️
Let's build a word counter machine (function)! 🛠️
We give it a sentence, and it gives back the word count! 🤖
function countWords(sentence) {
let words = sentence.split(' ');
return words.length;
}
console.log(countWords('I am a coder'));
console.log(countWords('yes'));Use Your Machine 🤖
Now we can count any sentence super easily! 🤖
Just call countWords and it does all the work! 🎉
function countWords(sentence) {
return sentence.split(' ').length;
}
console.log('Words: ' + countWords('the cat sat on the mat'));
console.log('Words: ' + countWords('coding rocks'));A Friendly Report 📊
Let's make the counter say a friendly message! 📊
This makes our Word Counter feel like a real app! 😄
function countWords(sentence) {
let count = sentence.split(' ').length;
console.log('📝 Your sentence: ' + sentence);
console.log('🔢 Word count: ' + count + ' words!');
}
countWords('learning to code is awesome');Count Three Sentences 📚
Let's count three different sentences in a row! 📚
See how each one gets its own count! 🔢
function countWords(sentence) {
return sentence.split(' ').length;
}
console.log('A: ' + countWords('hi there'));
console.log('B: ' + countWords('one two three four'));
console.log('C: ' + countWords('word'));You Can Count Words! 🏆
Brilliant! 🏆 You built a real word counter!
- ✂️ Split the sentence into a list
- 📏 Use
.lengthto count the pieces - 🛠️ A function makes it reusable
Next, we count letters too! 🔠
Quick Check ✅
Brain time! 🧠 How do we find out how many words are in our list?
Lesson Recap 🎀
Super job, counter! 🎀 Today you learned:
- ✂️ Split the sentence first
- 📏
.lengthcounts the words - 🛠️ A
countWordsfunction is handy
Next lesson: counting letters! 🔠
Frequently asked questions
Is the “Counting Words” lesson free?
Yes — the full text of “Counting Words” is free to read here on the web, and the Javascript for Kids course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Javascript for Kids course, upgrade to CoddyKit PRO.
What will I learn in “Counting Words”?
Count the pieces. You practise Javascript for Kids with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Javascript for Kids?
No prior experience is required. Javascript for Kids on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Counting Words” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Javascript for Kids lesson?
Yes. Every Javascript for Kids lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Splitting Text
- Counting Words
- Counting Letters
- Longest Word