Javascript Basics

How to get current full year using Javascript?

In some cases we may need to complete a task in loop. To handle such situation, we have for loop in Javascript.

For Loop

Lets begin with an example to understand the need of “for loop”

Example

Lets print 1 to 10

Solution:

<script>

var num=1;

console.log(num);

num++ // incremented the value by 1

console.log(num);

num++ // incremented the value by 1

console.log(num);

num++ // incremented the value by 1

console.log(num);

num++ // incremented the value by 1

console.log(num);

num++ // incremented the value by 1

console.log(num);

num++ // incremented the value by 1

console.log(num);

num++ // incremented the value by 1

console.log(num);

num++ // incremented the value by 1

console.log(num);

num++ // incremented the value by 1

console.log(num);

num++ // incremented the value by 1

</script>

 

Output:

1

2

3

4

5

6

7

8

9

10

(What is console.log? Google chrome browser and firefox browser has inbuild console which can be used to print output. Just right click on a webpage and click inspect. Then look for console. In the above example, we are printing to the console)

In the above example, the approach is straight. We have incremented the value and printed it to the console. Here we have to write the code ten times. Now imagine, what if, a requirement came to print 1 to 1 lakh or 1 crore or 1 billion, then?

Solution: the task which are repetitive can be performed using for loop.

In the above example, the value of the variable num is incremented by 1 again and again, till 10 is printed. Increment task here is repetitive. Each repetition is a loop. So here 10 loops are there.

Therefore such loop task can be handled using “for loops”

Before using for loop, always ask following three questions to yourself.

  • What is the starting value or starting point?
  • How long should the loop continue?
  • How much the value should be incremented or decremented at the end of each loop?

Lets re-consider the above example, print 1 to 10

In this case, lets answer the above questions

What is the starting value?

The starting value is 1.

How long should the loop continue?

The loop should continue until unless the value is less than or equal to 10.

How much the value should be incremented at the end of each loop?

The value should be incremented by 1 at the end of each loop.

Now lets see the syntax

Syntax

for(starting condition;loop until;value increment or decrement){

// perform task

}

 

Note: The word for – for loop must begin with the word for. Then parenthesis. Inside parenthesis, we set three parameters. First one is the starting value. Second one is the condition upto when the loop should continue. The third one is the how much the value should increment or decrement. Then inside the chain bracket, we keep the required code that should be performed in the each loop.

Note: The semi-colons around the ‘loop until’ in the above syntax.

Lets see a real example.

Example

Lets print 1 to 10 using for loop.

<script>

for(num=1;num<=10;num++){

console.log(num);

}

</script>

 

Output

1

2

3

4

5

6

7

8

9

10

In the above example, num=1 (Inside the parenthesis) is the starting value.

;num<=10;  This is the condition upto when the loop should continue.

num++ Value increments at the end of each loop.

Example

Lets print 10 to 1 using for loop in reverse order

As I have mentioned earlier, before using for loop ask yourself three questions and then proceed.

What is the starting value?

The starting value is 10.

How long should the loop continue?

The loop should continue until unless the value is greater than or equal to 1.

How much the value should be incremented or decremented at the end of each loop?

The value should be decremented by 1 at the end of each loop.

Solution

<script>

for(num=10;num>=1;num–){

console.log($num);

}

</script>

 

Output

10

9

8

7

6

5

4

3

2

1

Lets try another example

Example

Print Even numbers between 1 to 10

Before using for loop, answer the three questions.

What is the starting value?

In this case, is the starting value 1? No. We want to print even numbers in between 1 to 10(2,4,6,8,10). Therefore the starting value is 2.

How long should the loop continue?

The loop should continue until unless the value is less than or equal to 10.

How much the value should be incremented at the end of each loop?

In this case, should the value increment by 1? No. At the end of each loop the value should be incremented by 2.

Solution

<script>

for(num=2;num<=10;num+=2){

console.log(num);

}

</script>

Output

2

4

6

8

10