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:

function factorial(num)

{

 

    if (num < 0)

    {

        return -1;

    }

    else if (num == 0)

    {

        return 1;

    }

    var tmp = num;

    while (num-- > 2)

    {

        tmp *= num;

    }

    return tmp;

}

 

var result = factorial(8);

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.

function factorial(num)

{

    if (num < 0)

    {

        return -1;

    }

    else if (num == 0)

    {

        return 1;

    }

    else

    {

        return (num * factorial(num - 1));

    }

}

 

var result = factorial(8);

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.

// Global definition of aCentaur.

var Cricket = "It's played between two teams,";

 

// A local aCentaur variable is declared in this function.

function x()

{

    var Cricket = "Sachin is God of Cricket";

}

x();

Cricket += " each team has eleven players.";

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 behaviour.

var aNumber = 100;

tweak();

 

function tweak()

{

    // This prints "undefined", because aNumber is also defined locally below.

    document.write(aNumber);

 

    if (false)

    {

        var aNumber = 123;

    }

}

Up Next
    Ebook Download
    View all
    Learn
    View all