JavaScript Loops Explained with Examples | For Loop
Learn how loops work in JavaScript with beginner-friendly examples. This complete guide covers for loops, while loops, and practical use cases like printing messages, iterating arrays, and filtering even/odd numbers.
🔁Introduction to Loops in JavaScript
Imagine someone asks you to print "Hello world".
You’d simply write:
console.log('Hello world') Now let’s say your friend wants you to print "Hello world" three times:
JavaScript
console.log('Hello world')
console.log('Hello world')
console.log('Hello world')
Still manageable, right?
But what if they ask for it 100 times, or even 500 times?
Repeating console.log('Hello world') over and over is time-consuming and impractical. This is where loops come in. Loops help you perform repetitive tasks efficiently and with clean code.
✅ JavaScript for Loop Example
Here’s how you can print "Hello world" 100 times using a for loop:
JavaScript
for (let i = 0; i < 100; i++) {
console.log('Hello world')
}
💡 When to Use Loops
Use a loop when you want to repeat a task multiple times with minimal code.
🔍 JavaScript for Loop Syntax
JavaScript
for (initialization; condition; increment / decrement) {
// code to run in each iteration
}
Breakdown:
Initialization: Where the loop starts (
let i = 0)Condition: The test that decides whether to keep looping (
i < 100)Increment/Decrement: How the counter changes after each loop (
i++)
📦 Example: Printing All Values in an Array
You can use a loop to go through all elements in an array:
JavaScript
let arr = [10, 20, 44, 55, 66, 22, 88, 99, 44]
for (let i = 0; i < arr.length; i++) {
console.log(arr[i])
}
🔍 Example: Find All Odd Numbers in an Array
Here’s how to print only the odd numbers from an array:
JavaScript
let arr = [10, 20, 44, 55, 66, 22, 88, 99, 44]
for (let i = 0; i < arr.length; i++) {
if (arr[i] % 2 === 1) {
console.log(arr[i])
}
}
🔍 Example: Find All Even Numbers in an Array
Similarly, here’s how to print only the even numbers:
JavaScript
let arr = [10, 20, 44, 55, 66, 22, 88, 99, 44]
for (let i = 0; i < arr.length; i++) {
if (arr[i] % 2 === 0) {
console.log(arr[i])
}
}
⏳ Coming Soon: While Loops in JavaScript
Besides the for loop, JavaScript also provides:
whileloopdo...whileloop
These are useful when the number of repetitions is unknown at the start. We'll cover them in a future tutorial.
📘 Summary
In this tutorial, you’ve learned:
The importance of loops in JavaScript
How to use the
forloopThe basic syntax and structure of a loop
Practical examples using arrays:
Printing each item
Filtering even and odd numbers
Using loops allows you to write cleaner, shorter, and more efficient code — especially for tasks that involve repetition.
📚 You will also Learn and Explore...
JavaScript
forloop tutorialHow
whileLoops work in JavaScriptLooping through arrays with
for,for...of, andforEachJavaScript beginner tips and tricks
Finding even and odd numbers using loops
Happy coding — and keep on looping! 🔁
JavaScript for Loop – In Details
Master JavaScript for loops with clear, step-by-step examples and dry runs! Learn how for loops work, from basic syntax to array iteration, filtering even/odd numbers, and common mistakes. Perfect for beginners, students, and developers learning JavaScript fundamentals.
Read Full StoryWhat is a Function in JavaScript? | Function Examples & Guide
Functions in a JavaScript. Learn what functions are in JavaScript with examples. This beginner-friendly guide covers function declarations, parameters, return values, and more.
Read Full StoryWhat is JavaScript? A Complete Beginner's Guide
Learn what JavaScript is, how it works, and how to add it to your web pages with inline, external, async, and defer script loading strategies.
Read Full StoryIntroduction to Node.js (Full Interview Guide)
Explore Node.js with our concise infographic detailing its core functionalities. Learn how this JavaScript runtime powers fast, scalable backend development, real-time apps, and robust APIs. Perfect for developers and businesses looking to understand Node.js benefits.
Read Full Story