• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / Basics / Compress For Loops

Compress For Loops

Compress For Loops

Use the built-in forEach() method to loop through an array with one line of code:

const numbers = [1, 2, 3, 4, 5];

// LONGER FORM
for(let i = 0; i < numbers.length; i++){
  console.log(numbers[i]);
}

// SHORTHAND
numbers.forEach(number => console.log(number));

Source

https://medium.com/geekculture/20-javascript-snippets-to-code-like-a-pro-86f5fda5598e

Basics

Related Snippets:

  • JavaScript Operators Explained
  • The onclick Event in JavaScript
  • The onabort Event in JavaScript
  • Assignment Operators in JavaScript

Primary Sidebar

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2025 JavaScriptSource.com

  • About
  • Privacy Policy
  • FAQ
  • Jobs For Developers