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.

Neon image

Next.js applications: Set up a Neon project in seconds

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Get started →

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay