- A variable declared outside a function, becomes GLOBAL.
- A global variable has global scope: All scripts and functions on a web page can access it.
Code:-
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h1>Global Variable</h1>
<p>A global variable can accessed from any where </p>
<input type="button" onclick="cal_fun1();" value="Function 1">
<input type="button" onclick="cal_fun2();" value="Function 2">
<script>
var carname = "bus";
function cal_fun1()
{
alert(typeof(carname));
}
function cal_fun2()
{
alert(typeof(carname));
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h1>Global Variable</h1>
<p>A global variable can accessed from any where </p>
<input type="button" onclick="cal_fun1();" value="Function 1">
<input type="button" onclick="cal_fun2();" value="Function 2">
<script>
var carname = "bus";
function cal_fun1()
{
alert(typeof(carname));
}
function cal_fun2()
{
alert(typeof(carname));
}
</script>
</body>
</html>
What is a Global variable ?
Reviewed by Network security
on
June 08, 2019
Rating:
No comments: