Part -2 Control Flow Statement

작성자

카테고리:

← 피드로
DEV Community · Guna Ramesh · 2026-06-25 개발(SW)

Guna Ramesh

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

원문에서 계속 ↗

코멘트

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다