This website collects cookies to deliver better user experience
Alternative to if/else blocks
Alternative to if/else blocks
Introduction
We often overuse if/else blocks. We use them throughout our code base every time we want to run optional block of codes or return different outputs based on an input value or a condition.
Example
Let's say I have a function that takes an input (string) and modifies that string.
To keep things simple, the function receives a week number, week 1 and should return week one
Using an if/else conditionals, we can have code like so