DEV Community

Guna Ramesh
Guna Ramesh

Posted on

Part -2 Control Flow Statement

why we use break in Switch cases?
If the break statement is not used, JavaScript continues executing the next cases even when a match is found. This behavior is called fall-through.

example
let day = 1;
switch (day) {
case 1:
console.log("sunday")

        case 2:
            console.log("monday")

        case 3:
            console.log("tuesday")
        default:
            console.log("invalid day")
Enter fullscreen mode Exit fullscreen mode

output
sunday
monday
tuesday
invalid day
Default
The default block is optional in a switch statement. It is used to execute code when none of the cases match the given value. Without a default block, no code runs if there is no matching case.

Ternary Operator
A ternary operator is a shorthand way of writing a simple if...else statement in JavaScript.
It is called ternary because it uses 3 parts:

condition ? valueIfTrue : valueIfFalse
Enter fullscreen mode Exit fullscreen mode

program

let age = 20;
let result = age >= 18 ? "Adult" : "Minor";

console.log(result);

output:
Adult
Enter fullscreen mode Exit fullscreen mode

Top comments (0)