how to write a bash loop in javascript

In JavaScript, you can use a for loop to replicate the functionality of a for loop in Bash.

Here's an example of a for loop in Bash:

for i in {1..5}; do
   echo "Welcome $i times"
done
52 chars
4 lines

Here's how you can write the same loop in JavaScript:

index.tsx
for (let i = 1; i <= 5; i++) {
   console.log(`Welcome ${i} times`);
}
71 chars
4 lines

The loop in JavaScript uses the same structure as the loop in Bash. The loop initializes i to 1, runs the loop as long as i is less than or equal to 5, increments i by 1 after each iteration, and executes the loop code block (in this case, logging to console) each time.

related categories

gistlibby LogSnag