GithubHelp home page GithubHelp logo

working-with-loops's Introduction

Writing For Loops in JavaScript

Loops

  • In JavaScript, a for loop is a control structure that allows you to execute a block of code repeatedly for a specified number of iterations. It's a fundamental loop construct used to automate repetitive tasks and iterate through a sequence of values, typically an array or a numerical range.
for (initialization; condition; iteration) {
   // Code to be executed in each iteration
   // This code block can contain one or more statements
}

// Code outside the loop
  • Here's how the components of the for loop work:
  1. Initialization: This part is used to initialize a loop control variable (usually called an index or a counter). It's executed before the loop starts and is typically used to set the initial value of the control variable.
  2. Condition: The condition is a boolean expression that determines whether the loop should continue or stop. It's evaluated before each iteration, and if the condition is true, the loop continues; if it's false, the loop terminates.
  3. Iteration: The iteration part is executed after each iteration of the loop. It's typically used to update the loop control variable (e.g., increment or decrement the index).
  4. Code Block: The code block contains the statements that are executed in each iteration of the loop. This is where you place the code that you want to repeat.
  5. Code outside the loop: This code is executed after the loop finishes executing. It's outside the scope of the loop.
for (let i = 0; i < 5; i++) {
   console.log("Iteration:", i);
}
  • In this example, the loop will execute five times because the condition i < 5 starts as true and becomes false after the fifth iteration (when i becomes 5). The output will be: Iteration: 0 Iteration: 1 Iteration: 2 Iteration: 3 Iteration: 4

  • For loops are incredibly versatile and are commonly used to iterate over arrays, generate sequences, perform calculations over time, and much more. They're a powerful tool for automating repetitive tasks in your JavaScript programs.

Prep

  1. Connect your js file to your HTML file
  2. Open your code in the browser and open your console

Activities

  1. In the js file, complete the list of exercises.

working-with-loops's People

Contributors

lhack47 avatar techbasedb avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.