Urgent.News

What's breaking now, across thousands of outlets.

Tech

Loops in JavaScript

What Are Loops? Loops are used to execute a block of code repeatedly until a condition is no longer satisfied or the loop is otherwise stopped. For example, if we want to print numbers from 1 to 10, we don't need to write ten separate console.log() statements. We can use a loop instead. JavaScript provides different types of loops. Let's understand the most common ones. 1. for Loop A for loop is…

Loops in JavaScript are used to execute a block of code repeatedly until a specific condition is met or the loop is terminated. They prove useful when you want to print numbers, process arrays, perform calculations, or repeat tasks without duplicating code.

The most common types of loops in JavaScript include for, while, do...while, for...of, and for...in.

The for loop is employed when the number of repetitions is known beforehand. It comprises three parts: initializing a counter, setting a condition for the loop to continue, and incrementing or decrementing the counter. For example, the loop: for (let i = 1; i <= 5; i++) { console.log(i); } prints numbers from 1 to 5.

The while loop repeats as long as its condition remains true. It starts with a counter or any other variable before the loop and updates it within the loop. For instance, this loop: let i = 1; while (i <= 5) { console.log(i); i++; } also prints numbers from 1 to 5, but the counter is declared and updated inside the loop.

The do...while loop, on the other hand, executes the code block at least once before checking the condition. In the example: let i = 1; do { console.log(i); i++; } while (i <= 5); it prints numbers from 1 to 5, even if the condition is initially false.

The for...of loop is used to iterate over the values of an iterable, such as an array or string. For example: let fruits = ["Apple", "Banana", "Mango"]; for (let fruit of fruits) { console.log(fruit); } outputs "Apple", "Banana", and "Mango".

Lastly, the for...in loop is utilized to iterate over the enumerable property keys of an object. With an object like let student = { name: "John", age: 22, course: "CSE" }; the loop: for (let key in student) { console.log(key, student[key]); } prints "name John", "age 22", and "course CSE". For arrays, the for...of loop is generally more suitable when you need to access each value.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

How a WordPress WhatsApp plugin talks to Meta's Cloud API

Adding WhatsApp to a WordPress site usually means a chat button or a separate SaaS dashboard. A third route is a plugin that talks to Meta's WhatsApp Business Cloud API straight from your own server.

  • WordPress site can use WhatsApp plugin communicating directly with Meta's Cloud API
  • Plugin receives customer messages via webhook, processes replies through REST API
  • Contacts, conversations, consent records stored securely in WordPress database

I Built an Offline Contract Risk Analyzer That Flags the Clauses That Cost You the Most

Last month a friend signed a 12-month SaaS contract without reading clause 14. It auto-renewed for another year and the early-termination fee was three months of subscription.

  • ClauseGuard is a Python-based offline contract risk analyzer.
  • Tool flags high-risk clauses like auto-renewal traps and uncapped indemnities.
  • ClauseGuard generates HTML reports for contract review before signing.

Your Image CDN Picks the Format. Here's How to See What It Actually Sends

Here is one image URL from a real Shopify store, requested three times. Same URL, same second, three different files: https://jellinator.com/cdn/shop/files/ShotCups_100Pack.png?width=1200 Chrome…

  • Image CDNs choose image formats based on browser Accept headers.
  • Vary: Accept header indicates format changes according to browser.
  • Browser-like Accept headers may not always be used, affecting results.

More from Monday 12 October →