JAVASCRIPT VARIABLES
VARIABLES
A JavaScript variable is simply a name of storage location.
There are two types of variables in JavaScript : local variable
and global variable.
There are some rules while declaring a JavaScript variable
(also known as identifiers).
1. Name must start with a letter (a to z or A to Z),
underscore( _ ), or dollar( $ ) sign.
2. After first letter we can use digits (0 to 9), for example
value1.
3. JavaScript variables are case sensitive, for example x and X
are different variables.
VARIABLES
Correct JavaScript variables
1.var x = 10;
2.var _value="sonoo";
Incorrect JavaScript variables
1.var 123=30;
2.var *aa=320;
VARIABLES
Correct JavaScript variables
1.var x = 10;
2.var _value="sonoo";
Incorrect JavaScript variables
1.var 123=30;
2.var *aa=320;
EXAMPLE OF JAVASCRIPT VARIABLE
<script>
var x = 10;
var y = 20;
var z=x+y;
document.write(z);
</script>
JAVASCRIPT LOCAL VARIABLE
A JavaScript local variable is declared
inside block or function. It is accessible
within the function or block only. For
example: <script>
function abc(){
var x=10;//local variable
}
</script>
Or,
<script>
If(10<13){
var y=20;//JavaScript local
variable
}
</script>
JAVASCRIPT GLOBAL VARIABLE
<html>
A JavaScript global variable is accessible <body>
from any function. A variable i.e. declared
outside the function or declared with window <script>
object is known as global variable. For var data=200;//gloabal variable
example: function a(){
document.writeln(data);
}
function b(){
document.writeln(data);
}
a();//calling JavaScript function
b();
</script>
</body>
</html>
JAVASCRIPT GLOBAL VARIABLE
<html>
A JavaScript global variable is accessible <body>
from any function. A variable i.e. declared
outside the function or declared with window <script>
object is known as global variable. For var data=200;//gloabal variable
example: function a(){
document.writeln(data);
}
function b(){
document.writeln(data);
}
a();//calling JavaScript function
b();
</script>
</body>
</html>
ACTIVITY 01
Directions: Using Visual
studio or Solo learn,
perform the following
programming codes
below:
<html>
<body>
ACTIVITY 01
<script>
var data=200;//gloabal variable
function a(){
document.writeln(data);
}
function b(){
document.writeln(data);
}
a();//calling JavaScript function
b();
</script>
</body>
</html>
ACTIVITY 01
<script>
var birthday=1999
var recentyear=2022
var age=recentyear-birthday;
document.write(age)
</script>
THANK YOU!