Learning JavaScript tutorial – JavaScript break, continue and Labels

Learning JavaScript tutorial – JavaScript break, continue and Labels

JavaScript break statement

JavaScript break is a loop control statement. Break statements are commonly found inside the loop body; break helps us to control the execution of a loop. JavaScript Break statement (break keyword) may be used to break the flow and exits out of the loop structures of while loop, do-while loop, for loop, if condition and switch statement.

There are certain conditions when we need to break out of a loop before the complete loop gets executed. Or, in some cases we want to break out of the loop because there may be chances of an error. With the execution of the break statement, program flow will resume to the next sequential statement following the loop.

JavaScript break statement is an integral part of a switch case statement.
Break brings control out, as the statements in the switch case executed. If the break statement were omitted from switch case, the interpreter would execute each and every statement in all the switch case structures.

<script type="text/javascript" language="javascript" >
var x = prompt("Write a number in between 1 to 4");
switch (x)
{
  case "1": alert(‘x is 1’);
          break;
  case "2": alert(‘x is 2’);
          break;
  case "3": alert(‘x is 3’);
          break;
  case "4": alert(‘x is 4’);
          break;
  default: alert(‘x is not 1, 2, 3 or 4’);
}
</script>

Continue reading “Learning JavaScript tutorial – JavaScript break, continue and Labels”

Learning JavaScript tutorial – JavaScript for (loop) Statement

Learning JavaScript tutorial – JavaScript for (loop) Statement

JavaScript for loop is on of the most commonly used loop statement which provides a looping construct that is often more convenient than the while loop statement. JavaScript for loop statement allows you to repeat a section of code a certain number of times; changing values of variable counter by increment or decrement it each time the code is executed.

JavaScript for loop is the most compact form of looping which includes for loop initialization, for loop test case statement, and loop control statement all in one line. The JavaScript for loop statement initialise variable counter at the start of the execution of the loop. Then it checks if the expression evaluates to true, then only for loop statement execute the body of the loop; means the piece of code between the curly brackets and change the value of variable counter by increment or decrement it. For loop perform some action on a piece of code till the test case condition statement is satisfied or evaluate true.

JavaScript for loop statement Syntax

for (loop initialization, loop test case statement, loop control statement)
{
  statement block executed while conditional expression
  evaluates to true(satisfied).
}

Here, If the resulting value of test case conditional expression is true or it is evaluated true, for loop execute statement block enclosed within the Curly braces – {} and new value of variable counter is calculated.

JavaScript for Statement Flowchart
for loop statement flowchart

Continue reading “Learning JavaScript tutorial – JavaScript for (loop) Statement”

Learning JavaScript tutorial – JavaScript while(loop), do…while(Loop) statement

Learning JavaScript tutorial – JavaScript while(loop), do…while(Loop) statement

JavaScript while statement

In JavaScript, while loop statement allows us to perform some action on a piece of code till the condition is satisfied or true. The JavaScript while loop statement check condition at the start of the execution of the loop. If the expression evaluates to true, the while loop statement execute the body of the loop; means the piece of code between the curly brackets.

JavaScript while statement Syntax

while(conditional expression is true)
{
  statement block executed while conditional expression
  evaluates to true(satisfied).
}

Here, If the resulting value of conditional expression is true or it evaluated to true, while loop statement block enclosed within the Curly braces – {} is executed.

JavaScript While loop Statement Flowchart
do while loop statement flowchart

Continue reading “Learning JavaScript tutorial – JavaScript while(loop), do…while(Loop) statement”

Learning JavaScript tutorial – Understanding JavaScript Variable Types

Learning JavaScript tutorial – Understanding JavaScript Variable Types

JavaScript Variable is the best mean of storing the data (value). The JavaScript is a loosely typed programming languages and do not need to define the type of a variable while defining it. The main advantage of JavaScript Variable is that it can held any kind of data values, or types, which include number, string, Boolean, and null.

JavaScript allows us to assigned any variable with any value. This feature of javascript provides great flexibility in coding. In this article we are going to Understand the JavaScript Variable Types. If you want to learn some basic about the JavaScript Variables please go through "Learning JavaScript tutorial – JavaScript Variables".

Continue reading “Learning JavaScript tutorial – Understanding JavaScript Variable Types”

Learning JavaScript tutorial – JavaScript with Statement

Learning JavaScript tutorial – JavaScript with Statement

JavaScript offer lots of verity whiling dealing with the statements. Along with the flow control and loop control statements JavaScript supports object-related "with statements". With statement allows using a shorthand notation when referring objects and deals with the object scope chain.

with (object)
{
  statement(s);
}

Let us consider an example of the document object allows us to access the objects that compose a document. Using JavaScript with Statement we can access the scope of document object.

Continue reading “Learning JavaScript tutorial – JavaScript with Statement”