1. JavaScript Tutorial
  2. Loop For & While
  3. Math Object
  4. What is Array
  5. What is JSON
  6. Function in JS
  7. RegExp – Regular Expression

Loop For & While

Loops can execute a block of code a number of times.

  • for – loops through a block of code a number of times
  • for/in – loops through the properties of an object
  • for/of – loops through the values of an iterable object
  • while – loops through a block of code while a specified condition is true
  • do/while – also loops through a block of code while a specified condition is true

For Loop

for (expression 1; expression 2; expression 3) {
  // code block to be executed
}
  • Expression 1 is executed (one time) before the execution of the code block. It’s a optional parameter
  • Expression 2 defines the condition for executing the code block. It’s also a optional but you must use break in that case;
  • Expression 3 is executed (every time) after the code block has been executed.
  • Break statement

Example :

var max = 5
for (var i=0;i<max;i++) {
  gs.info('Rohit , i value: '+i);
}

For in & For of Loop

  • The JavaScript For in statement can also loop over the properties of an Array:
  • For of let you loop over iterable data structures such as Arrays, Strings, Maps, NodeLists, and more.
var numbers = [45, 4, 9, 16, 25];
for (var x in numbers) {
  gs.info(numbers[x]);
}

Loop for JSON:

var json = {
    "name": "John",
    "age": 22,
    "gender": "male",
};

for(var key in json){
gs.info('Key '+key+' value '+json[key]);
}

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *