The Smart Coder and the Magic of JavaScript Control Statements

The Smart Coder and the Magic of JavaScript Control Statements

·

3 min read

In a small town, Ravi, a curious young coder, is staring at the screen of the laptop. He had recently learned about the JavaScript and trying to code a simple online shop for his uncle’s sweet shop, Gupta ji ki Mithaiya. But there was a problem — his website needed to decide when to offer discounts, repeat certain tasks, and handle multiple customer request efficiently.

Ravi sighed, sipping on his chai. “There must be a smarter way to do this!”. That’s when he stumbled upon control statements — the secret behind making JavaScript smarter.

if-else : The wisdom of the shopkeeper

Ravi’s uncle, Gupta Ji, had a simple rule for discounts :

• If a customer buys sweets worth ₹500 or more, they get a 10% discount.

• Otherwise, no discount.

That sounds like an if-else statement!” Ravi realized. He quickly wrote this code :

if (totalCost >= 500) {
    console.log("You get a 10% discount")
}
else {
    console.log("No discount")
}

Now, the website can decide when to give a discount — just like Gupta Ji!

if-else if

If Gupta Ji want to expand the range of prices to get more discounts, they can do this using if-else if.

if (totalCost > 400 && totalCost <= 500) {
    console.log("You get a 10% discount")
}
else if (totalCost > 500 && totalCost <= 700) {
    console.log("You get a 15% discount")
}
else {
    console.log("No discount")
}

Nested if-else

Ravi want to include a feature of website, which ask people if they want “Cash on Delivery“ or “Advance Payment” when they click on “Book the Mithai“.

if (orderPlaced) {
    if (CashOnDelivery) {
        console.log("You choosed cash on delivery.")
    }
    else {
        console.log("Pay using UPI")
    }
}
else {
    console.log("What are you waiting for? Buy mithai")
}

Switch : The Art of Handling Multiple Requests

One day, customers started requesting sweets based on festivals. Some wanted Kaju Katli for Diwali, others asked for Gulab Jamun for weddings, and some preferred during Durga Puja.

“Handling all these choices using multiple if-else statements would be messy,“ Ravi thought. That’s when he found the switch statement, which made his code cleaner.

switch (festival) {
    case "Diwali" :
        console.log("Kaju Katli is the perfect choice!")
        break
    case "Wedding" :
        console.log("Gulab Jamun will make your celebration sweeter!")
        break
    case "Durja Puja" :
        console.log("Rasgullas are a must have!")
        break
    default :
        console.log("We have sweets for every occasion!")
        break
}

Now, the website could automatically suggest sweets based on festivals — just like a smart shopkeeper!

Conclusion : Ravi Becomes a Master Coder

As Ravi finished coding, he looked at his screen with pride. His website wase intelligent, capable of making decisions, and handling multiple conditions smoothly.

His uncle patted his back, saying, “Beta, you’ve made my mithai shop as smart as a big city store!“

Ravi smiled, sipping his chai. He had learned an important lesson — coding is just like running a mithai shop. If you control the flow well, everything runs smoothly!

And from that day on, he wasn’t just a coder — he was a JavaScript Control Flow Master!