Variable and Function definition
So now we are learning about variable, their types, type cast, function creation and calls.
Variables: It is simple to define the variables using the 'var' keyword, We can define the type at runtime i.e. it can be string, integar, array or even a function to a variable.
Interesting part is public and private variables. The public variables are kept alive even when the function has ended and available outside of the function, The private variables ends in the function and dies with the function.
Here are the ways to define public and private variables in the function
function dynFunction() {
first = 'Public'; //This is public variable definition which will stay on even when function has ended
var second='Private'; // var keyword makes this private
}
You can execute the function in the script and the public variable value will remain available for usage.
See the simplicity, Ah i even created a function in the above example, Here is my hello world program which now contains the above function and test for public variable
<html>
<Head> Sabby learning scripting <br> </head>
<body>
<div id=dynHello></div>
<div id=dynVar> </div>
<div id=dynVar2> </div>
<script type='text/javascript'>
document.getElementById('dynHello').innerHTML='<p>Hello World!</p><br>'
function dynFunc() {
first='Public'; //This is public variable definition which will stay on even when function has ended
var second='Private'; // var keyword makes this private to this function
}
dynFunc();
document.getElementById('dynVar').innerHTML=first;
document.getElementById('dynVar2').innerHTML=second;
</script>
</body>
</html>
So now we are learning about variable, their types, type cast, function creation and calls.
Variables: It is simple to define the variables using the 'var' keyword, We can define the type at runtime i.e. it can be string, integar, array or even a function to a variable.
Interesting part is public and private variables. The public variables are kept alive even when the function has ended and available outside of the function, The private variables ends in the function and dies with the function.
Here are the ways to define public and private variables in the function
function dynFunction() {
first = 'Public'; //This is public variable definition which will stay on even when function has ended
var second='Private'; // var keyword makes this private
}
You can execute the function in the script and the public variable value will remain available for usage.
See the simplicity, Ah i even created a function in the above example, Here is my hello world program which now contains the above function and test for public variable
<html>
<Head> Sabby learning scripting <br> </head>
<body>
<div id=dynHello></div>
<div id=dynVar> </div>
<div id=dynVar2> </div>
<script type='text/javascript'>
document.getElementById('dynHello').innerHTML='<p>Hello World!</p><br>'
function dynFunc() {
first='Public'; //This is public variable definition which will stay on even when function has ended
var second='Private'; // var keyword makes this private to this function
}
dynFunc();
document.getElementById('dynVar').innerHTML=first;
document.getElementById('dynVar2').innerHTML=second;
</script>
</body>
</html>