javascript data types and variable scope tutorial

JavaScript Data Types:

JavaScript Data Types are the type of values that can be represented and manipulated by JavaScript.

JavaScript Data Types List:

1. Numbers.
2. Strings.
3. Boolean.
4. Null.
5. Undefined.
6. Object.
7. Array.
8. RegExp.

JavaScript variables:

Variable is the name of reserved memory location. In JavaScript var keyword is used to declare a variable.

Rules for declaring a JavaScript variable:

1. JavaScript variable name must begin with a letter, underscore, or dollar sign.
2. JavaScript variable names are case sensitive.
3. JavaScript reserved keywords like abstract, boolean etc can’t be used as JavaScript variable name.

JavaScript variable scope:

1. Local variable.
2. Global variable.

JavaScript Local variable:

A variable defined inside a function or block of JavaScript code is known as a local variable. It can be accessed only within that function or block.

JavaScript Local Variable Example:

<html>
<head> 
<script>  
function showNumber(){
//local variable    
var num=10;
document.write(num);  
}  
showNumber();
</script> 
</head>
<body>
</body>
</html>

Try it:

JS Bin on jsbin.com

JavaScript Global variable:

A variable defined outside a function or block of JavaScript code is known as a global variable. It can be accessed anywhere in JavaScript code

JavaScript Global Variable Example:

<script>  
<html>
<head> 
<script>  
//Global variable    
var num=10;
function showNumber(){
document.writeln(num);  
}  
function displayNumber(){
document.writeln(num);  
}  
showNumber();
displayNumber();
</script> 
</head>
<body>
</body>
</html>

Try it:

JS Bin on jsbin.com