Tuesday, October 21, 2014

javascript variable scope and datatypes

JAVASCRIPT VARIABLES:
- Variable is container for storing the information.
- Variable names are case sensitive. (x and X are two different variable).
- Variable names must be started with letter or underscore.
- To declare variable in JavaScript var statement is used.
- Syntax: VAR <variable name>
NOTE: it is not compulsory to declare variable in JavaScript before its use.
- If you assign values to variables that have not yet been declared, the variables will  
  automatically be declared.

 <html><body>
<script language = "JavaScript">
var x = 10; < - - - - - variable x is declared.
var y = 20; < - - - - - variable y is declared.
z = x+y;      < - - - - - variable z is not declared.
document.write('addition is' + z);
</script>
</body>
</html> (semicolon in JavaScript is optional)

Scope/Life-Time of variable in JavaScript:
- A variable declared within a JavaScript function becomes LOCAL and can only be  
accessed within that function. (The variable has local scope).
- You can have local variables with the same name in different functions, because local
variables are only recognized by the function in which they are declared.
- Local variables are destroyed when you exit the function.
- Variables declared outside a function become GLOBAL, and all scripts and functions
on the web page can access it.
- Global variables are destroyed when you close the page.
- If you declare a variable, without using "var", the variable always becomes GLOBAL.

Data Types in Java Script:
1)Number:Integer Number,Float Number,NaN
2)String:it include charachter and words
3)Boolean:return TRUE or FALSE


No comments: