This article analyzes the scope of variables in js function. Share it for your reference. The specific analysis is as follows:
Let's look at a function example:
Copy the code as follows: <html>
<head>
</head>
<body>
<script type="text/javascript">
var a = 5;
var c = 3;
function t(){
var a = 6;
var b = 10;
document.write(a+'-----'+b);
document.write(c);
}
t();
</script>
</body>
</html>
Unlike php, if a variable is to be called in a js function, it first looks up from the function. For example, a is equal to 6; and c cannot be found in the function body, so it will look up outside the function body, find it and call it, if it cannot be found, an error will be reported.
I hope this article will be helpful to everyone's JavaScript programming.