The scope of a variable refers to the portions of the program where it can be directly accessed.Let's see some examples.
A variable declared within a function is called a local variable, its value is recognized only within that function, it doesn't exist out of that function. Thus, another function can declare a variable with same name, JS (JavaScript) treats the two as different variables.
The variables created outside the functions can be used everywhere in that script, inclusively inside the functions and other JS script from that HTML page.
<script> function ex1() { var va = 8; } document.write(va); </script>
<script> var va = 'tutorials'; function ex2() { var va = 'lesson'; document.write(va+ '<br>'); } // Calling the function ex2(); // lesson document.write(va+ '<br>'); // tutorials (uses the 'va' declared outside function) </script>
<script> var va = 5; function ex3() { va += 3; // the same as ( va = va+3; ) document.write(va+ '<br>'); } // Calling the function ex3(); // 8 document.write(va+ '<br>'); // 8 </script>
When we create a function and define the number of its arguments, we must keep them in mind when calling that function.
But, there are situations where we have to transmit a different number of arguments when we call the function, which may be lower or higher than the number of function parameters.
if you neglect to include arguments in your function call, the interpreter will not view this as a syntax error. The interpreter assigns a value of undefined
to the missing arguments.
The value of undefined
is evaluated to false when tested as a Boolean.
undefined
we test to see if each parameter is not defined (using the Boolean ! not), in that case we create a local variable by that name and by the time you get to the line beginning with return, every argument is defined and has a default value, if not already available.<script> function sumNrs(a,b,c) { if (!a) a = 0; if (!b) b = 0; if (!c) c = 0; return a + b + c; } var sum1 = sumNrs(1, 2); document.write('<p>'+sum1+'</p>'); </script>
function fName(p1=v1, p2=v2){ //code to be executed }- 'p1' and 'p2' are the parameters; 'v1' and 'v2' are the default values for each one.
<script> function sumNrs(a=0, b=0, c=0){ return a + b + c; } var sum1 = sumNrs(1, 2); document.write('<p>'+sum1+'</p>'); </script>
In some cases the function can receive more arguments than its parameters. The values of additional arguments are not lost, they are stored in an Array called arguments
, this object is a local property of all functions.
- For example, the arguments passed to a function called setMsg() are stored in the object-like Array setMsg.arguments.
length
property:
<script> function addNumbers() { // Sets a variable that will return the amount var re = 0; // go through the arguments Array and adds every value to 're' variable for(var i=0; i<addNumbers.arguments.length; i++) { re += addNumbers.arguments[i]; } return re; } document.write('1+2+3+4+5+6+7+8 = '+ addNumbers(1,2,3,4,5,6,7,8)); </script>
You can create and use a function inside another function. This is known as function nesting.
- Example, inside a sumAB() function it's defined another function that is called when the sumAB() returns its result:
<script> function sumAB(a,b) { // Get the amount of 'a' and 'b' var re = a+b; // Nested function function multiply(x) { return x*x; } // Return the call of nested function multiply(), with the variable 're' to argument return multiply(re); } document.write( sumAB(3,4) ); // 49 </script>
<script> function factorial(n) { var re; if (n>0) re = n*factorial(n-1); else if (n==0) re = 1; else re = null; return re; } document.write('<p>Factorial 8 = '+ factorial(8) +'</p>'); </script>
<ul> <li>http://coursesweb.net/html/</li> <li>http://coursesweb.net/css/</li> </ul>
.some_class { display: list-item; }
var obj = { "courses": ["php", "javascript", "ajax"] }; var jsonstr = JSON.stringify(obj); alert(jsonstr); // {"courses":["php","javascript","ajax"]}
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>'; $dochtml = new DOMDocument(); $dochtml->loadHTML($strhtml); $elm = $dochtml->getElementById("dv1"); echo $elm->nodeValue; // CoursesWeb.net