Switch case vs array in JavaScript [closed]

1 day ago 5
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\]()

Filburt's user avatar

Filburt

18.1k14 gold badges106 silver badges167 bronze badges

Niaina Franco ROBERTO's user avatar

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

Read Entire Article