JavaScirpt Tutorial Index

JavaScript Tutorial Javascript Example Javascript comment Javascript Variable Javascript Data Types Javascript Operators Javascript If Else Javascript Switch Statement Javascript loop JavaScript Function Javascript Object JavaScript Arrays Javascript String Javascript Date Javascript Math Javascript Number Javascript Dialog Box Javascript Window Object Javascript Document Object Javascript Event Javascript Cookies Javascript getElementByID Javascript Forms Validation Javascript Email Validation Javascript Password Validation Javascript Re-Password Validation Javascript Page Redirect Javascript Print

Misc

JavaScript P5.js JavaScript Minify JavaScript redirect URL with parameters Javascript Redirect Button JavaScript Ternary Operator JavaScript radio button checked value JavaScript moment date difference Javascript input maxlength JavaScript focus on input Javascript Find Object In Array JavaScript dropdown onchange JavaScript Console log Multiple variables JavaScript Time Picker Demo JavaScript Image resize before upload Functional Programming in JavaScript JavaScript SetTimeout() JavaScript SetInterval() Callback and Callback Hell in JavaScript Array to String in JavaScript Synchronous and Asynchronous in JavaScript Compare two Arrays in JavaScript Encapsulation in JavaScript File Type Validation while Uploading Using JavaScript or jquery Convert ASCII Code to Character in JavaScript Count Character in string in JavaScript Get First Element of Array in JavaScript How to convert array to set in JavaScript How to get current date and time in JavaScript How to Remove Special Characters from a String in JavaScript Number Validation in JavaScript Remove Substring from String in JavaScript

Interview Questions

JavaScript Interview Questions

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;
There are two types of variables:
  1. Local Variable
  2. Global Variable
Local Variable- It is declared inside block or function. Example
<script>  
functionabc(){  
var x=10; //local variable  
</script>
Global Variable-It has global scope which means, it can be defined anywhere in JavaScript code. A variable is declared outside the function. Example
<!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>
Try Now Declaring JavaScript global variable within function To declare JavaScript global variable inside function, we need to use window object. Example
window.value=90;
Now it can be declared inside any function and can be accessed from any function. Example
<!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>
Try Now