The gym is opening morning bookings through its old widget, booking-widget.js, written in 2014 with var throughout, though its strings have since been updated to template literals. var was the only way to declare a variable before let and const arrived in 2015, so you’ll meet it in old code, answers, and tutorials. Before the widget takes morning calls, you need to catch the bug it hides.
Read var as a Function-Wide Name
A let or const belongs to its block, but a var belongs to the whole function, as Python’s names do. So a var set inside an if is visible after it.
Two more differences show up in old code. A second var declaration of a name refers to the same variable, so a long function can overwrite its own value without any warning, where a second let in the same scope is a SyntaxError.
And a top-level var in a module stays in the module, but an old-style script, such as a classic browser script or Node’s REPL, puts it on globalThis, the global object, where any other script can overwrite it.
Fall Into the Silent undefined
The widget’s bandFor() dates from evening-only booking. Python’s band_for("morning") would stop with UnboundLocalError: cannot access local variable 'prefix' where it is not associated with a value. Given that and the temporal dead zone, predict the output of the morning call: