Javascript Variable
Variables are used to store values (name="Ram") or expressions (Sum=x+y). Before using of variable first we need to declare it. We use keyword var to declare a variable like this:
var name;
- Local Variable
- Global Variable
<script>
functionabc(){
var x=10; //local variable
</script>
<!DOCTYPE html>
<html>
<body>
<script>
var data =200; //global variable
function a(){
document.writeln(data);
}
function b(){
document.writeln(data);
}
a();//calling javascript function
b();
</script>
</body>
</html>
window.value=90;
<!DOCTYPE html>
<html>
<body>
<script>
function a(){
window.value=50; //declare global variable by use of window object
}
function b(){
alert(window.value);//access global variable from other function
}
a();
b();
</script>
</body>
</html>