Search This Blog

Tuesday, 31 January 2017

Write a javascript program to demo of local and global variables.

JS Prac - 2

<html>

<head>
    <script>
    lname = "Gajjar"; //global
    </script>
</head>

<body>
<pre>
1. A variable that is declared inside a function using the var keyword, will have a local scope.
2. A variable that is declared inside a function without var keyword, will have a global scope means acts like a global variable.
</pre>

<script>
var fname = "Karan"; //global
myFunction();

function myFunction() {
    var h = "hello"; //local
    document.write(h+" "+fname+" "+lname);
}
</script>
</body>
</html>

No comments:

Post a Comment