I have been playing around with dynamic assignment of routes in express and stumbled into unexpected behavior from for ... of loop.
My idea was simple:
Create simple array
const roomsArray = ["Algebra", "Backend", "Frontend"];
loop through that array and create a route for each item in array, so I tried with for ... of loop
router.get(`/room/${i}`, isUser, (req, res) => { res.render("index/room", { path: "index/room", name: i }); }) } ```
And the result was more than unexpected(to me at least). Each route was created as expected, but whichever route I would test, value of i
assigned to name
was always value of last item in array.
Switching over to for loop or forEach solves the issue and everything behaves as expected, however I would like to know why does this behavior occur using for ... of statement?