By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Control Statement and loops
JavaScript

Control Statement and loops

Rohan750
Last updated: 2021/05/16 at 5:35 AM
Rohan750
Share
5 Min Read

Control Statement and loops in javascript


 1)If…Else

The if statement executes a statement if a specified condition is true. If the condition is false, another statement can be executed.

Example:


var tomorrow = 'rain';

if(tomorrow == 'rain'){
  console.log('take a raincoat');
}else{
  console.log('No need to take a raincoat');
}

output:

Contents
 1)If…Else2) switch Statementbreakcontinue3)While Loop Statement4)Do-While Loop Statement5)For Loop

PS C:UsersThe-BeastDesktopjavascript Blog> node index.js        
take a raincoat
PS C:UsersThe-BeastDesktopjavascript Blog> 

Example:


var tomorrow = 'no rain';

if(tomorrow == 'rain'){
  console.log('take a raincoat');
}else{
  console.log('No need to take a raincoat');
}

output:


PS C:UsersThe-BeastDesktopjavascript Blog> node index.js        
No need to take a raincoat
PS C:UsersThe-BeastDesktopjavascript Blog>

2) switch Statement

Evaluates an expression, matching the expression’s value to a case clause, and executes statements associated with that case.
1st without a break statement.

Example: Find the Area of circle, triangle, and rectangle?

 

var area = "circle" ;
var PI = 3.142, l=6, b=5, r=4;

if(area == "circle"){
  console.log("the area of the circle is : " + PI*r**2);
}else if(area == "triangle"){
  console.log("the area of the triangle is : " + (l*b)/2);
}else if(area == "rectangle"){
  console.log("the area of the rectangle is : " + (l*b));
}else{
  console.log("please enter valid data");
}

output:


PS C:UsersThe-BeastDesktopjavascript Blog> node index.js        
the area of the circle is : 50.272
PS C:UsersThe-BeastDesktopjavascript Blog> 

using break statement

Example:


var area = "circle" ;
var PI = 3.142, l=6, b=5, r=4;

switch(area){
  case 'circle': 
    console.log("the area of the circle is : " + PI*r**2);
    break;

  case 'triangle':
    console.log("the area of the triangle is : " + (l*b)/2);
    break;

  case 'rectangle':
    console.log("the area of the rectangle is : " + (l*b));
    break;

  default:
    console.log("please enter valid data");
}   

output:


PS C:UsersThe-BeastDesktopjavascript Blog> node index.js        
the area of the circle is : 50.272
PS C:UsersThe-BeastDesktopjavascript Blog> 

break

Terminates the current loop, switch, or label statement and transfers program control to the statement following the terminated statement.

continue

Terminates execution of the statements in the current iteration of the current or labeled loop, and continues execution of the loop with the next iteration.

3)While Loop Statement

The while statement creates a loop that executes a specified statement as long as the test condition evaluates to true.

Example:


var num=0;

while(num <= 10){
  console.log(num); 
  num++;
}

output:


PS C:UsersThe-BeastDesktopjavascript Blog> node index.js        
0
1
2
3
4
5 
6 
7 
8 
9 
10
PS C:UsersThe-BeastDesktopjavascript Blog> 

4)Do-While Loop Statement

The do…while loop is similar to the while loop except that the condition check happens at the end of the loop. This means that the loop will always be executed at least once, even if the condition is false.

Example:


var num = 20;
do{
  console.log(num); 
  num++;
}while(num <= 10);

output:


PS C:UsersThe-BeastDesktopjavascript Blog> node index.js        
20
PS C:UsersThe-BeastDesktopjavascript Blog> 

5)For Loop

Looping in programming languages is a feature that facilitates the execution of a set of instructions/functions repeatedly while some condition evaluates to true. A Loop executes the sequence of statements many times until the stated condition becomes false.

Example:


for(var num = 0; num <= 10; num++){
    console.log(num);
}

output:


PS C:UsersThe-BeastDesktopjavascript Blog> node index.js        
0
1 
2 
3 
4 
5 
6 
7 
8 
9 
10
PS C:UsersThe-BeastDesktopjavascript Blog> 

What are truthy and falsy values in Javascript?

we have a total of 5 falsy values in javascript.

0,” “,undefined,null,NaN,false.

Example:



if(score = 0){
    console.log("Yay, We won the game ");
}else{
    console.log("OMG, we loss the game ");
}

output:


PS C:UsersThe-BeastDesktopjavascript Blog> node index.js        
OMG, we loss the game 
PS C:UsersThe-BeastDesktopjavascript Blog> 

Example:



if(score = 50){
    console.log("Yay, We won the game ");
}else{
    console.log("OMG, we loss the game ");
}

output:


PS C:UsersThe-BeastDesktopjavascript Blog> node index.js        
Yay, We won the game 
PS C:UsersThe-BeastDesktopjavascript Blog> 

watch the video:https://youtu.be/KGkiIBTq0y0

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article Expressions and operators
Next Article Functions in JavaScript
10 Comments 10 Comments
  • Dominique says:
    August 14, 2021 at 10:58 pm

    Hello, I want to subscribe for this weblog to take newest updates, so where can i do it please help out.

    Reply
  • Kian says:
    August 17, 2021 at 9:23 pm

    You are so cool! I do not suppose I have read something like this before.

    So great to discover somebody with some unique thoughts on this subject.
    Really.. many thanks for starting this up.

    This website is one thing that is required on the web, someone with a bit of originality!

    Reply
  • Natisha says:
    August 19, 2021 at 1:28 am

    Very nice article, totally what I was looking for.

    Reply
  • t-shirt says:
    August 31, 2021 at 9:37 pm

    Οh my g᧐odness! Amazing article dude! Many thanks, However
    I am experiencing troublеs with your RSS. I don’t know the reason why I am unable to join it.
    Іs tere anyonne еlse haѵing identical RSS issues? Anyone thɑt
    knows the answer can you kindly rеspߋnd? Thanx!!

    Reply
  • champions says:
    October 5, 2021 at 2:31 am

    Thank you, I have just been looking for info approximately this
    topic for a long time and yours is the best I have came
    upon so far. But, what concerning the conclusion? Are you
    sure in regards to the source?

    Reply
  • shaco build - shaco runes + item guide for lol says:
    October 5, 2021 at 11:28 am

    What’s up, I want to subscribe for this website to take most up-to-date updates, thus where can i do it
    please help.

    Reply
  • Maribel says:
    November 6, 2021 at 8:46 pm

    Hello very nice blog!! Man .. Excellent .. Amazing ..
    I’ll bookmark your site and take the feeds also? I’m happy to seek out a lot of useful info right
    here within the put up, we want develop
    extra techniques on this regard, thank you for
    sharing. . . . . .

    Reply
  • Darla says:
    November 17, 2021 at 12:38 am

    When I originally commented I clicked the “Notify me when new comments are added” checkbox and now each time a
    comment is added I get four e-mails with the same comment.
    Is there any way you can remove me from that service?
    Thanks a lot!

    Reply
  • Bill says:
    November 18, 2021 at 11:47 pm

    Its like you learn my thoughts! You appear to understand so much about this, like you wrote
    the guide in it or something. I feel that you can do with a
    few percent to pressure the message home a bit, but instead of that, that is fantastic blog.
    An excellent read. I will definitely be back.

    Reply
  • Elba says:
    November 22, 2021 at 5:40 pm

    That is a great tip especially to those fresh to the blogosphere.
    Short but very precise information… Appreciate your sharing
    this one. A must read post!

    Reply

Leave a Reply Cancel reply

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

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?