Any variable is used, it should be declared using var keyword, but the code doesn't show any error and might user doesn't affect. But variable declaration is must.
If you assign a value without declare using var keyword, the variable is consider as global variable, so it should affect in big projects.
Code:-
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script>
/*If you are not used var keyword when declare a value, the variable is consider as global variable.*/
(function()
{
a = 90;
})()
function fun1()
{
//Previously not used var keyword for a variable, so it is visible globally.
alert(a);
}
</script>
</head>
<body>
<button onclick="fun1();">Call Function</button>
</body>
</html>
Don't forget car keywords:- JavaScript
Reviewed by
Network security
on
November 13, 2019
Rating:
5
No comments: