var is used to declare variable, it is not mandatory to declare using var keyword.
But we strongly recommend you to declare the variable using var keyword.
Local variable can only be accessed within the function.
In the below example, when you press "Click me 2" button, it produce "Uncaught ReferenceError: a is not defined" error, since variable a is not declared inside the function (fun_name2). So the alert box won't appear.
Global variable are declared outside the function and it can be accessed anywhere in the web page.
We strongly recommended to use var keyword while declaring variables.
If you do not use var keyword the variable will be automatically considered as a global variable.