This article illustrates the importance of Var in JavaScript. Share to everyone for your reference. The specific analysis is as follows:
JavaScript's VAR function is to declare variables.
In general, there will be no error, but some cases will have different results if they are not written. Let's look at the following example:
<div id= "A" ></div>
<script type= "Text/javascript" >
a = 1;
alert (a);
</script>
This example above in the FF Chrome implementation will not be a problem, can output 1. But what about running in IE? Error: "object doesn ' t supportthis property or method."
Because IE can get a reference to a DOM element directly by ID, A=1 will make an error because at this point A is the DOM element of id= "a".
If the <div id= "a" ></div> this sentence is removed, there will be no problem. To avoid this conflict, it is recommended that you declare variables with var.
I hope this article will help you with your JavaScript programming.