Picking a Random Item
Choose a random thing from a list.
Picking a Random Item is a free Javascript for Kids lesson on CoddyKit. This is lesson 3 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, and your progress syncs across the web and the CoddyKit app. The Javascript for Kids course includes 4 lessons in total.
Pick a Surprise! 🎁
What if the computer picked a random snack for you? 🍕🍦🍪
Let us learn to pick a random item from a list! 📋
A List of Things 📋
An array is a list. We hold many things in one box! 📦
let snacks = ["pizza", "cookie", "apple"];
console.log(snacks);Items Have Spots 🔢
Each item has a spot number, starting at 0! 🧮
pizza is 0, cookie is 1, apple is 2. 😄
let snacks = ["pizza", "cookie", "apple"];
console.log(snacks[0]);Pick by Spot 👉
We grab an item using its spot number in [ ]. 🎯
let snacks = ["pizza", "cookie", "apple"];
console.log(snacks[2]);How Long Is It? 📏
.length tells us how many items are in the list! 🔢
let snacks = ["pizza", "cookie", "apple"];
console.log(snacks.length);A Random Spot 🎲
Remember random dice? We pick a random spot the same way! 🎰
let snacks = ["pizza", "cookie", "apple"];
let spot = Math.floor(Math.random() * 3);
console.log(spot);Grab the Random Item! 🎁
Now use that random spot to pick a snack! 🍪
let snacks = ["pizza", "cookie", "apple"];
let spot = Math.floor(Math.random() * 3);
console.log(snacks[spot]);Use .length for Safety 🛡️
Instead of typing 3, use .length. It works for any list size! 🎯
let snacks = ["pizza", "cookie", "apple"];
let spot = Math.floor(Math.random() * snacks.length);
console.log(snacks[spot]);Make a Picker Spell 🪄
Let us put it in a function to pick from ANY list! ✨
function pick(list) {
let spot = Math.floor(Math.random() * list.length);
return list[spot];
}
console.log(pick(["red", "blue", "green"]));Pick Anything! 🌈
Now you can pick a random color, animal, or name! 🐶🐱🐰
Press Run again for a new surprise! 🎉
You Are a Picker Pro! 🎉
Great work! You pick random items from lists now. 📋
This is super useful for games!
Quick Check 🧠
Question time! 🎯
List Wizard! 🏆
Hooray! You can pick random items! 🎁
Next, we build a lucky number game! 🚀
Learn JavaScript with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 35
- Lessons
- 140
Frequently Asked Questions
Is the “Picking a Random Item” lesson free?
Yes — the full text of “Picking a Random Item” is free to read here on the web. 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. The Javascript for Kids course includes 4 lessons in total.
What will I learn in “Picking a Random Item”?
Choose a random thing from a list. 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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
How long does the “Picking a Random Item” 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
- Making Random Numbers
- Rolling a Dice
- Picking a Random Item
- A Lucky Number Game