Skip to content
Advertisement

What is the best option, a loop inside a conditional or a conditional inside a loop?

Seems obvious, but let’s say we have 2 arrays and I want to push some objects inside if certain condition is true. Generally speaking, it would be better like this:

JavaScript

or like this:

JavaScript

I think the second option looks shorter, but maybe it’s worse in performance.

Advertisement

Answer

The best is:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement