Reader Level:
ARTICLE

JavaScript Statements

On October 19, 2012 Articles | JavaScript
Today, we are going to see the use of JavaScript Statements that are very important for starting with the fundamentals of JavaScript.
  • 1
  • 0
  • 2392

JavaScript Statements

Today, we are going to see the use of JavaScript Statements that are very important for starting with the fundamentals of JavaScript.

JavaScript block statement

JavaScript block statements are the curly braces ( { } ) you see everywhere. They are used to establish code that is to be compartmentalized into a specific function or statement.

The lines of code inside of a block statement is often intended to represent that they are part of that block statement.

The following are some examples showing the curly braces establishing blocks of compartmentalized code.

<script>

    if (3 < 5)

    {

        // code for this block statement goes in here

    }

    for (var i = 0; i < 10; i++)

    {

        // code for this block statement goes in here

    }

    function myFunction()

    {

        // code for this block statement goes in here

    }

</script>


TIP: You may come across code that processes just fine without the block statements in place (no curly brace nesting).

This is because JavaScript will execute all lines of code as one statement until it hits a semicolon( ; ), which is the statement break symbol in JavaScript and many other languages.

<script>

    if(3 < 5)

        document.write("EXPRESSION RETURNS TRUE")

    else

        document.write("EXPRESSION RETURNS FALSE");

</script>


block-statement-in-javascript.png

JavaScript break statement   

The JavaScript break statement is used to terminate a loop, switch or label statement from further processing.

Apply it when you want to force one of those types of statements to stop processing.

Example of terminating a loop using break

<script>

    for (i = 0; i < 20; i++)

    {

        if (i >= 5)

        {

            break;

        }

        document.write("Pass index " + i + " of the loop<br />");

    }

</script>


terminating-loop-statemnet-in-javascript.png


Using break above we were able to take a loop that has a ceiling of 20 passes, and force it to stop processing after 5 passes.

Example of terminating a switch statement using break

If break is not applied to cases of a switch statement, the switch statement will continue processing until its end. 

<script>

    var browser = "Chrome";

    switch (browser)

    {

        case "IE":

            document.write("Internet Explorer makes life hard for webmasters.");

            break;

        case "Chrome":

            document.write("Chrome confirms to popular browser standards.");

            break;

        case "Firefox":

            document.write("Firefox is bulky but developer friendly.");

            break;

        default:

            document.write("No information for browser: " + browser);

    }

</script>


terminating-switch-statement-in-javscript.png

JavaScript continue statement 

The JavaScript continue statement is used to bypass specified iterations of a loop so that the code in the loop statement does not execute for the specified iterations, and moves on to the next.

<script>

    for (i = 0; i < 20; i++)

    {

        if (i < 10)

        {

            continue;

        }

        document.write("Pass index " + i + " of the loop<br />");

    }

</script>


continue-statement-in-javascript.png

JavaScript do...while statement

The JavaScript do...while statement is an inverted while statement. It will execute code as long as the while condition returns a value of true.

In contrast to other loops the first iteration of the do...while loop is always guaranteed to run due to the condition evaluation being at the bottom. 

<script>

    var i = 0;

    do

    {

        document.write("Loop index: " + i + "<br />");

        i++;

    }

    while (i < 5);

</script>


do_while-statement-in-javascript.png

JavaScript for statement

The JavaScript for statement is a loop mechanism that will execute code as long as the condition evaluation continues to be true.

It stops running when the condition returns false. You establish three parameters inside of its parenthesis:

  1. Create the iteration variable (usually named "i")

  2. Establish the condition to evaluate each pass of the loop

  3. Iterate (increase) the variable by 1 each pass of the loop

<script>

    for (var i = 0; i < 5; i++)

    {

       document.write("Pass index " + i + " of the loop<br />");

    }

</script>


for-statement-in-javascript.png

JavaScript for...in statement

The JavaScript for...in statement is a loop mechanism that will iterate over all of the items in an array, and execute code for each item.

You establish two parameters inside of its parenthesis: 

<script>

    var peopleArray = new Array("Bill", "Jane", "Fred", "Sara");

    for (var person in peopleArray)

    {

       document.write(peopleArray[person] + " is in the array<br />");

    }

</script>


for-in-statement-in-javascript.png

JavaScript if...else statement

The JavaScript if...else statement is used for evaluation logic. Where any condition returns a value of "true", the if / else statement will stop processing and execute code nested in that winning condition.

The if statement always goes on top and starts the evaluations. The if statement can also be used by itself without the else and else if.

The optional multiple else if statements go in between the if and the else and allow you to evaluate many more conditions in that particular evaluation group.

The else statement always goes on the bottom and comes into play if no conditions are met (final clause).

<script>

    var a = 5;

    var b = 3;

    if (a < b)

    {

      document.write(a + " is less than " + b);

    }

    else if (a > b)

    {

      document.write(a + " is greater than " + b);

    }

    else

    {

      document.write(a + " must therefore be equal to " + b);

    }

</script>


if-else-statement-in-javascript.png

JavaScript return statement

The JavaScript return statement is used to return a value from a function. It terminates the function from further executing code, and returns a value to the caller.

<script>

    function addNums(v1, v2)

    {

      return v1 + v2;

    }

    var sum = addNums(2, 3);

    document.write(sum);

</script>


return-statement-in-javascript.png

JavaScript switch statement

The JavaScript switch statement evaluates an argument you pass through it for a match against a specific set of values that are defined using case labels.
 

Break stops the switch statement from further processing if a match is found, if you leave it out the switch statement will continue running.

 

Default is similar to the else statement in which it is a final clause when no evaluation returns a true value.

 

<script>

    var country = "USA";

    switch (country)

    {

        case "UK":

            document.write("UK citizens are known for being drunk.");

            break;

        case "USA":

            document.write("USA citizens are known for being dumb.");

            break;

        default:

            document.write("No match for value: " + country);

    }

</script>


switch-statemnet-in-javascript.png

JavaScript try...catch statement

The JavaScript try...catch statement is used to try code, and catch any subsequent errors that might exist in that code. The catch keyword will catch the error, and also execute code. 

<script>

    try

    {

      doSomething();

    }

  catch (e)

    {

      document.write(e);

    }

</script>


try-catch-statement-in-javascript.png

I get a Reference Error exception thrown when trying to run the doSomething() function above because that function has not been defined in my script.

Write a function called doSomething() in order to see that the function will run when you "try" it and the catch will be bypassed. 

JavaScript while statement

The JavaScript while statement is a loop mechanism that will execute code as long as the while condition returns a value of true. It stops running when the condition returns false.

<script>

    var i = 0;

    while (i < 5)

    {

      i++;

      document.write("Pass " + i + " of the loop<br />");

    }

</script>


while-statement-in-javascript.png

Summary

Through this article you will become familiar with the basics of JavaScript.

COMMENT USING

Trending up