JavaScript Loop Types - A Comprehensive Guide

Sun Jan 1, 2023

Here are a few examples of JavaScript programs that involve looping:

  • A program that uses a for loop to print the numbers from 1 to 10:
for (let i = 1; i <= 10; i++) { console.log(i); }
  • A program that uses a while loop to print the numbers from 1 to 10:
let i = 1; while (i <= 10) { console.log(i); i++; }
  • A program that uses a for-of loop to iterate over an array of numbers and print each one:
let numbers = [1, 2, 3, 4, 5]; for (let number of numbers) { console.log(number); }
  • A program that uses a forEach() method to iterate over an array of numbers and print each one:
let numbers = [1, 2, 3, 4, 5]; numbers.forEach(function(number) { console.log(number); });
  • A program that uses a map() method to create a new array with the squares of all numbers in an array
let numbers = [1, 2, 3, 4, 5]; let squares = numbers.map(function(number) { return number * number; }); console.log(squares);

DCT Academy
Full Stack web development training institute in Bangalore

Launch your GraphyLaunch your Graphy
100K+ creators trust Graphy to teach online
𝕏
Best MERN Full Stack Development Course | Bangalore | Offline | Online | 100% Placements | Training Institute | Front End Course | React JS Training | Online Course India 2024 Privacy policy Terms of use Contact us Refund policy