|
Answer» Global variables are WIDELY used and do not create any SECURITY concern.
But Global variables should be avoided in JavaScript because these kinds of variables can be easily overwritten by others scripts. On adding a NEW variable with the same name can create errors if we are not cautious about its placement. To avoid such confusions, it is a good practice to avoid global variables.
Use closures and avoid the usage of global variables. The process is to work with local variables and wrap the code in closures as shown below: <!DOCTYPE html>
<html>
<body>
<h2>Department</h2>
<p ID="myid"></p>
<script>
var department = {
id: 5,
name : "Finance",
Location : "North"
};
document.getElementById("myid").innerHTML = department.id + " is " + department.name + " department's id.";
</script>
</body>
</html>
|