Control structure, loops and functions

If-else

In the below code, three conditions are checked for the variable ‘age’; and corresponding message is printed.

  
            1 // asset/js/my_javascript
2 // if-else
3 age = 10;
4 if (age > 3 && age < 6){
5 document.write("Age : " + age + "<b> go to kindergarten</b>");
6 }
7 else if ( age >=6 && age < 18){
8 document.write("Age : " + age + "<b> go to school</b>");
9 }
10 else{
11 document.write("Age : " + age + "<b> go to college</b>");
12 }
13 document.write("<br>");

Since age is 10, therefore ‘else if’ statement is satisfied and we will get below output.

Code output:

 
            Age : 10 go to school

Switch-case-default

Below is an example of Switch-case-default statement.

  
	        1 // asset/js/my_javascript
2
3 // switch-case
4 var grade = 'A';
5 document.write("Grade " + grade + " : ");
6 switch(grade){
7 case 'A':
8 document.write("Very good grade!");
9 break;
10 case 'B':
11 document.write("Good grade!");
12 break;
13 default: // if grade is neither 'A' nor 'B'
14 document.write("Enter correct grade");
15 }
16 document.write("<br>");

Below is the output of above code.

 
	        Grade A : Very good grade! 

For loop

Below code prints the value from 5-0.

  
    	    1 // for loop
2
3 for (i=5; i>=0; i--){
4 document.write(i + " ");
5 }
6 document.write("<br>");

Below is the output of above code.

 
	        5 4 3 2 1 0 

While loop

Below code prints the value from 0-4.

  
    	    1 // while loop
2
3 x=0;
4 while (x < 5){
5 document.write(x + " ");
6 x++;
7 }
8 document.write("<br>");

Below is the output of above code.

 
	       0 1 2 3 4

Do-while

Below code prints the value from 0-2.

  
    	    1 // do-while
2
3 x=0;
4 do{
5 document.write(x + " ");
6 x++;
7 }while(x < 3);
8 document.write("<br>");

Below is the output of above code.

 
	       0 1 2

For-in loop

Below code prints the value from 0-2.

  
    	    1 // for-in loop
2
3 arr = [10, 12, 31]; // array
4 for (a in arr){
5 document.write(arr[a] + " ");
6 }
7 document.write("<br>");

Below is the output of above code.

 
	       10 12 31

Continue and break

Continue and break statements are used inside the ‘loop’ statements.

Continue will skip the loop and go to next iteration, if the condition is met. In the below code, 3 will not be printed at the output.

  
    	    1 // continue
2
3 for (i=5; i>=0; i--){
4 if (i==3){ // skip 3
5 continue;
6 }
7 document.write(i + " ");
8 }
9 document.write("<br>");

Below is the output of above code.

 
	       5 4 2 1 0

‘Break’ statement will quit the loop if the condition is met. In the below code, the for loop will be terminated at ‘i=3’, therefore only 5 and 4 will be printed.

  
    	    1 // break
2
3 for (i=5; i>=0; i--){
4 if (i==3){ // exit loop when i=3
5 break;
6 }
7 document.write(i + " ");
8 }
9 document.write("<br>");

Below is the output of above code.

 
	       5 4 

Functions

In the below code a function ‘add2Num’ is defined which adds the two numbers and returns the result.

  
    	    1 // continue
2
3 function add2Num(num1, num2){ // function definition
4 return num1 + num2;
5 }
6 sum = add2Num(2, 3); // function call
7 document.write("2 + 3 = " + sum);
8 document.write("<br>");

Below is the output of above code.

 
	       2 + 3 = 5

© Copyright 2025 | FolkLight Studios