ARTICLE AD BOX
I made a script that have a variable number in it that number goes from 0 to 5 and for each value between 0 to 5 I want to execute a statement.
Which one is better approach using a switch case or add five function reference in an array and call the functions using the number as index?
Ex :
let num = 2 const arr = \[func0, func1, func2\] arr\[num\]()18.1k14 gold badges106 silver badges167 bronze badges
2

The answer to your question would depend on what you mean by 'better' in this instance, For example - easier for people to understand/maintain in future or likely to be executed faster or ...
2026-02-08 21:04:20 +00:00
Commented 9 hours ago
Hello Franco, welcome, I don't think there is a noticeable difference in performance; it's more like “syntactic sugar.” When the quantity is large and conditions allow, I use the list of methods invoked in a for loop.
2026-02-08 21:18:52 +00:00
Commented 9 hours ago