Features of JavaScript in Visual Studio 2012:Recursion and Variable Scope

Introduction

 
In my previous article, you learned about one of the New features of JavaScript i.e. Typed Arrays. In today's article, you will learn about some more New Features of JavaScript in Visual Studio 2012 like Recursion and Variable Scope.
 
First, we will see the Recursion in JavaScript.
 
Recursion is an important programming technique in which a function calls itself, either directly or indirectly.
 
Step 1
 
First of all, add a JavaScript page to your Windows or Web Application. This can be done by right-clicking on your application and then "Add" | "New Item...".
 
prototype1.jpg
 
Now in the New Item list select the JavaScript Style Sheet.
 
prototype2.jpg
 
Step 2
 
Now, we will calculate the Factorial of a number by using a While Loop without recursion:
  1. function factorial(num)    
  2. {    
  3.      
  4.     if (num < 0)    
  5.     {    
  6.         return -1;    
  7.     }    
  8.     else if (num == 0)    
  9.     {    
  10.         return 1;    
  11.     }    
  12.     var tmp = num;    
  13.     while (num-- > 2)    
  14.     {    
  15.         tmp *= num;    
  16.     }    
  17.     return tmp;    
  18. }   
  19.   
  20. var result = factorial(8);  
  21. document.write(result); 
Its output would be 40320.
 
Step 3
 
You can make the preceding example recursive, instead of using the While Loop, simply calling the Factorial again. The recursion will stop when the value becomes 1.
  1. function factorial(num)  
  2. {  
  3.     if (num < 0)  
  4.     {  
  5.         return -1;  
  6.     }  
  7.     else if (num == 0)  
  8.     {  
  9.         return 1;  
  10.     }  
  11.     else  
  12.     {  
  13.         return (num * factorial(num - 1));  
  14.     }  
  15. }  
  16.    
  17. var result = factorial(8);  
  18. document.write(result); 
Its output will also be 40320 but it's a recursive function.
 
Step 4
 
Now you will learn about Scope. In JavaScript, there are two types of Scopes: Global and Local.
 
A variable that's declared outside the function is a global variable and its value is accessible and modifiable throughout the program.
 
A variable that's declared inside a function is local and its value is created and destroyed every time the function is executed.
 
A local variable can have the same name as a global variable but they would be entirely separate. If the value of one of them is changed then the other one is unaffected.
  1. // Global definition of aCentaur.  
  2. var Cricket = "It's played between two teams,";  
  3.    
  4. // A local aCentaur variable is declared in this function.  
  5. function x()  
  6. {  
  7.     var Cricket = "Sachin is God of Cricket";  
  8. }  
  9. x();  
  10. Cricket += " each team has eleven players.";  
  11. document.write(Cricket);  
// Output: "It's played between two teams, each team has eleven players".
 
Step 5
 
In JavaScript, variables are evaluated as if they were declared at the beginning of whatever scope they exist in. Sometimes this results in unexpected behavior.
  1. var aNumber = 100;  
  2. tweak();  
  3.    
  4. function tweak()  
  5. {  
  6.     // This prints "undefined", because aNumber is also defined locally below.  
  7.     document.write(aNumber);  
  8.    
  9.     if (false)  
  10.     {  
  11.         var aNumber = 123;  
  12.     }  


Similar Articles