You can use a one-liner for-loop in JavaScript by combining the initialization, condition, and increment/decrement statements in one line, separated by semicolons. Here's an example of a one-liner for-loop that runs a specific number of times based on an integer:
index.tsx49 chars2 lines
In this example, the loop runs 10 times because the condition i < 10
is true for the first 10 values of i
. You can replace 10
with any other integer to run the loop a different number of times. Here's another example that runs the loop 5 times:
index.tsx61 chars2 lines
This loop initializes i
to 0, checks if i < 5
, executes the code inside the loop (which logs "Hello World!" to the console), and increments i
by 1 on each iteration.
gistlibby LogSnag