Decision Making and Loops in JavaScript

Saqib Suleman
Mar 5, 2021

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");
}
}

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)
}

--

--