Skip to main content
Key2Goal

Main navigation

  • Home
  • About Us
  • Contact us
  • Glossary
  • Privacy Policy
User account menu
  • Log in

Breadcrumb

  1. Home

How to creating a FOR loop and a WHILE loop in JavaScript

Creating a for loop and a while loop in JavaScript using below simple example:

// Example: Printing numbers from 1 to 5 using a for loop
for (let i = 1; i <= 5; i++) {
  console.log(i);
}

In this example, the for loop iterates over the values of i from 1 to 5. It prints the value of i in each iteration using console.log().

While Loop:

// Example: Printing numbers from 1 to 5 using a while loop
let i = 1;
while (i <= 5) {
  console.log(i);
  i++;
}

In this example, the while loop checks the condition i <= 5 before each iteration. It prints the value of i and increments it by 1 (i++) within each iteration.

Both the for loop and the while loop are used for repetitive execution of code. The main difference is that the for loop has a built-in initialization, condition, and increment/decrement in a single line, whereas the while loop requires manual initialization and modification of the control variable within the loop block.

  • Log in to post comments

Comments

Article category

  • Drupal
  • Wordpress
  • Java
  • Javascipt
  • HTML
  • CSS
  • IOS
  • Android
  • PHP
  • MySQL
  • Other
  • Command line
  • NodeJs
  • Drupal 8
  • Drupal 7
  • GIT
  • DevOps
  • Python
  • Xampp
  • Server
  • Artificial intelligence
  • AppScript
  • Windows

Recent content

  • ERROR: Cannot create symbolic link : A required privilege is not held by the client.
    3 hours 33 minutes ago
  • Differences Between Bootstrap 3 & Bootstrap 4
    1 month ago
  • About Us
    1 month ago
  • Privacy Policy
    1 month ago
  • Create a Virtual Host on Xampp in 5 steps
    3 months ago
  • How to setup Multisite in drupal 9
    9 months ago
  • How to creating a FOR loop and a WHILE loop in JavaScript
    9 months ago
  • Calculator program HTML & PHP
    9 months ago
  • Algothrim to create a web application
    9 months ago
  • 10 Git Useful Commands for Every Developer
    9 months ago
© Copyright 2017 - 2026 key2Goal. All Rights Reserved