If variables are used repeatedly in a code block then its better to execute that code block in a function as much as possible to take advantage of the better performance of local function variables instead of executing that code block without function.

Global variables stay in a highly-populated namespace.
They are stored along with many other user-defined quantities and JavaScript variables.

Browser distinguishes between global variables and properties of objects that are in the current context.

In below code, variable c is used repeatedly in for loop.
So run below in your browser console and see that first for loop without function takes more time than the second for loop executed within function.