JavaScript Fundamentals Part-2

JavaScript Fundamentals Part-2

Check out JS Fundamentals Part-1 for better understanding!

Strict Mode

Strict mode can be activated in javascript by adding use strict; at the beginning of the javascript file. It allows us to understand errors properly.

let hasMoney = true;
if(hasmoney) {
  console.log("Has Money");
}

As hasMoney is true, it should print Has Money in the console but it doesn't because I have misspelt hasmoney on purpose, to show how strict mode helps.

If the strict mode was activated, it would've shown the error Uncaught ReferenceError: hasmoney is not defined

Loops

A sequence of instructions that is continually repeated until a certain condition is reached is called loops. A certain process is done, such as getting an item of data and changing it, and then some condition is checked such as whether a counter has reached a prescribed number.

There are mainly two types of loops:

  1. Entry Controlled loops: In this type of loop, the test condition is tested before entering the loop body. For Loop and While Loop is entry-controlled loops. For example: For loop, While loop.

  2. Exit Controlled Loops: In this type of loop the test condition is tested or evaluated at the end of the loop body. Therefore, the loop body will execute at least once, irrespective of whether the test condition is true or false. the do-while loop is exit controlled loop. For example: Do-while loop

For loop

A for loop is a repetition control structure that allows us to write a loop that is executed a specific number of times.

**Syntax: **

for (initialization; test expression; update expression)
{    
     // body of the loop
}

For example:

for (int i = 0; i < 5: i++){
  console.log("Hello World!");
}

Initialization Expression: In this expression, we have to initialize the loop counter to some value.
In the above example, int i=0;

Test Expression: In this expression, we have to test the condition. If the condition evaluates to true then we will execute the body of the loop and go to update expression otherwise we will exit from the for a loop.
In the above example, i <= 5;

Update Expression: After executing the loop body this expression increments/decrements the loop variable by some value.
In the above example, i++;

While loop

While loops are used in situations where we do not know the exact number of iterations of the loop beforehand.

Syntax:

initialization expression;
while (test_expression)
{
   // statements
  update_expression;
}

For example:

int i = 0;
while (i < 5)
{
   console.log("Hello World!");
  i++;
}

Do-while loop

In do-while loops also the loop execution is terminated on the basis of test conditions.
The main difference between a do-while loop and the while loop is in the do-while loop the condition is tested at the end of the loop body

**Syntax: **

initialization expression;
do
{
   // statements
   update_expression;
} while (test_expression);

For example:

int i = 0;
do
{
   console.log("Hello World!");
   i++;
} while (i < 5);

Not updating variable value will always make test expression true resulting in an infinite array, which might harm your system. Therefore, do not forget to update the value.

Happy reading!