- 01Setting Up (What You'll Need)
- 02Creating Variables
- 03Conditionals (if Statements)
- 04Loops (for Statements)
- 05Creating Functions
- 06Working with Arrays
- 07Working with Objects
- 08Loops (while Statements)
- 09Working with Strings
- 10Using Classes (Object-Oriented Programming)
- 11Error Handling (try...catch)
- 12Destructuring and the Spread Syntax
- 13Async Code (Promise / async & await)
- 14Advanced Array Methods (filter and reduce)
- 15The switch Statement
- 16The Ternary Operator
- 17Inheritance (Extending Classes)
- 18How to Write Comments
- 19Logical Operators (AND, OR, NOT)
- 20Constants (Making Read-Only Values with const)
- 21Splitting and Joining Strings (split and join)
- 22Null-Safe Syntax (?? and ?.)
- 23Searching Arrays and Collections (includes and find)
- 24Transforming Arrays with map
- 25Two-Dimensional Arrays (Table-Shaped Data)
- 26Building a Custom Error Class
- 27Default Arguments (Setting Initial Values for Parameters)
- 28Using Set (Collections)
- 29Verifying Correctness with assert (Your First Step Into Testing)
- 30Higher-Order Functions (Passing a Function as an Argument)
- 31Stacks and Queues (Basic Data Structures)
- 32The Basics of Type Conversion (Casting)
- 33Introduction to Regular Expressions (Pattern Matching)
- 34The Binary Search Algorithm
- 35Building a Caesar Cipher (a Letter-Shifting Cipher)
- 36Understanding How Bubble Sort Works
- 37Building and Displaying Dates (Basic Year/Month/Day Operations)
- 38Writing Several Unit Tests Together (Multiple Test Cases)
- 39Speeding Up Calculations with Memoization (Caching)
- 40Normalizing Strings (trim and Case Unification)
- 41The Difference Between Shallow Copy and Deep Copy
- 42The Basics of Enums (Enumerated Types)
- 43Flattening Arrays (flatten)
- 44Reversing a String and Checking for a Palindrome
- 45Pairing Up Two Arrays (a zip Operation)
- 46Rounding Numbers (floor, ceil, and round)
- 47Multi-Line Strings (Template Literals)
- 48Returning Multiple Values from a Function (Array Destructuring)
- 49Finding the GCD and LCM (the Euclidean Algorithm)
- 50Formatting Numbers (Digit Alignment and Decimal Precision)
- 51Cleanup Processing with try/catch/finally
- 52Writing Type-Agnostic, General-Purpose Functions
- 53The Basics of Map (an Object for Key-Value Pairs)
- 54Generating Random Numbers
- 55Bitwise Operations (AND, OR, XOR, and Shift Operations)
- 56Using static (Static Class Properties and Methods)
- 57Waiting a Fixed Amount of Time (setTimeout and await)
- 58Watch Out for Floating-Point Rounding Error
- 59Transforming and Flattening at Once with flatMap()
- 60FizzBuzz (the Classic Practice Problem)
- 61Checking Whether a Number Is Prime
- 62Set Operations with Set (Union, Intersection, and Difference)
- 63Converting Number Bases (Binary and Hexadecimal)
- 64Checking That Brackets Match (an Application of Stacks)
- 65Checking for an Anagram
- 66Checking Whether a Year Is a Leap Year
- 67Converting Temperature (Celsius ⇄ Fahrenheit)
- 68Finding the Prime Factorization
- 69[Applied] Build a Simple To-Do List Tool
Loops (for Statements)
In this lesson you'll learn how to write repeating logic with JavaScript's for statement, so you can run the same processing efficiently over and over. This is aimed at people searching "JavaScript for loop syntax" or "JavaScript loop for beginners."
When you want to repeat the same operation many times, you use a loop. A for statement repeats by specifying three parts: for (initial value; condition; update) { ... }. Instead of copy-pasting the same code 100 times, a loop lets you express a huge amount of processing in just a few lines — that's the power of a loop. The loop ends the moment the condition is no longer satisfied.
The sample code starts at let i = 1 and repeats 5 times while i <= 5, increasing i by 1 each time with i++. A for loop is also used very often to process the contents of an array one at a time, and it's the foundation for the array operations you'll learn about later — a genuinely essential piece of syntax.
A common stumbling block is forgetting to write the update part (i++) or getting the condition wrong, which creates an "infinite loop" that never ends. If the browser seems to freeze, check the condition and update parts first. Being off by one in the number of iterations (an "off-by-one error") is another very common mistake.
In real development, for loops show up in a huge range of situations: displaying a list of products from an array one by one on screen, or retrying a network request a fixed number of times. The ability to design what a loop's body actually does is an essential skill in everyday programming.
A for loop is often paired with an array like for (let i = 0; i < fruits.length; i++) — the classic pattern of repeating once per array element. If you tie the loop's stop condition to the array's length, the loop automatically repeats the right number of times even if the number of elements changes.
If you specify the number of repetitions with a variable, you get a more flexible loop like for (let i = 1; i <= count; i++). Once you can write loops whose repeat count changes depending on the situation, rather than a fixed number, the range of things you can build expands enormously.
💡 Anything passed to console.log() appears in the output below.
