MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
For Loop0%
Study Materials
JavaScript for BeginnersProgress 0%
1.1What is JavaScript?1.2How JavaScript Works in Browser1.3Setting up Environment: VSCode, Live Server
5.1For Loop5.2While and Do-While Loops5.3Loop Control: break, continue
HomeTutorialsJavaScript for BeginnersFor Loop
While and Do-While Loops

For Loop

Beginner12 min read•Updated: 2026-09-15•By Er. Sumit Kumar (MSK Institute)
Study Materials

Loops are programming constructs used to repeat a block of code multiple times until a specific condition is met. The classic for loop is the most versatile and widely used counting loop in JavaScript.


1. Anatomy of the Standard for Loop

A standard for loop consists of three expressions separated by semicolons:

1for (initialization; condition; increment/decrement) {
2 // Code executed repeatedly while condition is true
3}

Execution Step-by-Step:

  1. 1
    Initialization (let i = 0): Executes once before the loop begins. Sets up the counter variable.
  2. 2
    Condition (i < 5): Evaluated before every iteration. If truthy, the loop body runs; if falsy, the loop terminates.
  3. 3
    Loop Body (console.log(...)): The code inside { ... } executes.
  4. 4
    Increment (i++): Executes after the loop body. The counter increments and execution returns to Step 2.

2. Practical Use Cases for for Loops

A. Iterating Over an Array by Index

1const frameworks = ["React", "Vue", "Angular", "Next.js"];
2 
3for (let i = 0; i < frameworks.length; i++) {
4 console.log(`Index ${i}: ${frameworks[i]}`);
5}

B. Counting Down (Reverse Loop)

1for (let i = 10; i >= 1; i--) {
2 console.log(`Blastoff in: ${i}...`);
3}
4console.log("Lift off! 🚀");

C. Stepping by Increments Other Than 1

1// Even numbers up to 10:
2for (let i = 2; i <= 10; i += 2) {
3 console.log(i); // 2, 4, 6, 8, 10
4}

3. Nested for Loops

You can place loops inside loops (for example, traversing a 2D matrix or multiplication table):

1for (let i = 1; i <= 3; i++) {
2 for (let j = 1; j <= 3; j++) {
3 console.log(`${i} * ${j} = ${i * j}`);
4 }
5}
Important Warning
[!WARNING] Performance Alert: A nested loop with $N$ iterations inside a loop with $N$ iterations runs $N^2$ times ($O(N^2)$ time complexity). Keep nested loops shallow on large datasets!

4. The Infinite Loop Danger

If the condition never evaluates to false, the loop will run forever, consuming 100% CPU and crashing the browser tab:

1// ❌ CRASH DANGER: i will never be less than 0!
2for (let i = 0; i >= 0; i++) {
3 // Freezes the browser!
4}

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What are the three parts of a standard for loop header separated by semicolons?
Question 2
How many times does the loop for (let i = 0; i < 4; i++) execute?
Question 3
How do you loop through an array items backward from the last element to the first?
Question 4
What happens if the continuation condition in a for loop is omitted (e.g. for (;;) )?
Question 5
Why is let preferred over var when declaring the loop counter i?
Next Lesson

While and Do-While Loops

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Truthy & Falsy ValuesWhile and Do-While Loops

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master JavaScript for Beginners with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext