DEV Community

Cover image for Decision Making and Loops in JavaScript
Saqib Suleman
Saqib Suleman

Posted on

Decision Making and Loops in JavaScript

There are situations when we want to run the code at different conditions, such as, if one condition applies run a particular code, if another condition applies another code is run. This is prioritized with "if" and "else if" conditionals as follows:

function getColor(phrase){
   if (phrase === "stop"){
       console.log("red");
   } else if (phrase === "slow"){
       console.log("yellow");
   } else if (phrase === "go"){
       console.log("green");
   } else {
       console.log("purple");
   }
}
Enter fullscreen mode Exit fullscreen mode

We can run codes in multiple loops if we want to with the help of loops. With loops, our code runs until a certain condition is satisfied such as:

for (let i = 25; i >= 0; i-=5) {
    console.log(i)
}
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
tommus profile image
Thomas Smith

Nice! Just for future reference, you can wrap your code samples in three backticks (`) to make it look a little neater.