JavaScript Switch Statement Only Executes the First Case

I’m encountering an issue with my switch statement in JavaScript where it only processes the first case and skips the rest, even though I placed break statements correctly throughout.

I aim to compare today’s date with some specific dates in a dropdown menu. If any of those dates are earlier than today, I want to hide and disable those options. While I successfully employed if-else statements for this task, I thought a switch statement could offer a better performance.

Here’s my code:

var firstDate = new Date();
firstDate.setFullYear(2023, 2, 15);

var secondDate = new Date();
secondDate.setFullYear(2023, 2, 28);

var thirdDate = new Date();
thirdDate.setFullYear(2023, 3, 10);

var fourthDate = new Date();
fourthDate.setFullYear(2023, 3, 25);

var fifthDate = new Date();
fifthDate.setFullYear(2023, 4, 8);

var currentDate = new Date();
var dropdown = document.getElementById("DateSelector");

switch (true) {
case firstDate < currentDate:
    for (var j = 0; j < dropdown.length; j++)
    var option = dropdown.options[1];
    option.style.display = "none";
    option.disabled = true;
    break;

case secondDate < currentDate:
    for (var j = 0; j < dropdown.length; j++)
    var option = dropdown.options[2];
    option.style.display = "none";
    option.disabled = true;
    break;

case thirdDate < currentDate:
    for (var j = 0; j < dropdown.length; j++)
    var option = dropdown.options[3];
    option.style.display = "none";
    option.disabled = true;
    break;

case fourthDate < currentDate:
    for (var j = 0; j < dropdown.length; j++)
    var option = dropdown.options[4];
    option.style.display = "none";
    option.disabled = true;
    break;

case fifthDate < currentDate:
    for (var j = 0; j < dropdown.length; j++)
    var option = dropdown.options[5];
    option.style.display = "none";
    option.disabled = true;
    break;
}

What could be the reason only the first case executes? I appreciate any suggestions!

You’re misunderstanding how switch statements work. When you use switch (true), it runs the first case that’s true, then exits because of the break statement. That’s exactly what switch is supposed to do - handle one condition at a time, not multiple checks. You actually want all your date conditions checked and processed, so you need separate if statements for each comparison. Also, your code has syntax issues - the for loops are missing opening braces and you’re not even using the loop variable j. Just use individual if statements instead. Check each date condition separately and hide the dropdown options when needed. Skip the unnecessary loops entirely.

switch statements stop after the first match - that’s why only your first condition runs. You need regular if statements since you want to check all dates. Also, those for loops are broken - you’re not using the j variable and you’re missing curly braces.

Your switch (true) only runs the first case that’s true, then breaks out - that’s exactly how switch statements work. You need separate if statements instead since you want to check multiple conditions independently. Each date comparison should run regardless of what the others return. Also, you’ve got syntax errors in your for loops - missing the opening brace after the for statement. Plus you’re declaring a loop variable but never using it inside the loop, so you probably don’t need the for loop at all. Switch statements are meant for comparing one value against multiple options, not for evaluating a bunch of independent boolean conditions like you’re doing.