π§ Conditional Statement
Very often when you write code, you want to perform different actions for different decisions.
You can use conditional statements in your code to do this.
In JavaScript we have the following conditional statements:
Use
ifto specify a block of code to be executed, if a specified condition is trueUse
elseto specify a block of code to be executed, if the same condition is falseUse
else ifto specify a new condition to test, if the first condition is falseUse
switchto specify many alternative blocks of code to be executed
π° The if Statement
Use the if statement to specify a block of JavaScript code to be executed if a condition is true.
Syntax :
if (*condition*) {
// *block of code to be executed if the condition is true*
}Note that if is in lowercase letters. Uppercase letters (If or IF) will generate a JavaScript error. Example :
Make a "Good day" greeting if the hour is less than 18:00:
let hour = 15;
let greeting;
if (hour < 18) {
greeting = "Good day";
}
console.log(greeting);Good day<div class="infographic-jsifonly">
<style>
.infographic-jsifonly {
--ink: #1a1a1a;
--ink-soft: #5c5c5c;
--paper: #ffffff;
--paper-alt: #f0ece4;
--accent: #2563eb;
--accent-soft:#e3ebfd;
--line: #ddd6c9;
--ok: #1a7f4e;
--ok-soft: #e3f5ec;
--display-font: 'Fraunces', Georgia, serif;
--body-font: 'Inter', -apple-system, sans-serif;
--mono-font: 'IBM Plex Mono', 'Courier New', monospace;
max-width: 520px;
margin: 0 auto;
box-sizing: border-box;
font-family: var(--body-font);
color: var(--ink);
background: var(--paper);
}
.infographic-jsifonly[data-theme="dark"] {
--ink: #e8e6e1;
--ink-soft: #9a968c;
--paper: #121212;
--paper-alt: #1e1e26;
--accent: #5b8def;
--accent-soft:#202942;
--line: #2c313d;
--ok: #5cd6a0;
--ok-soft: #16342a;
}
.infographic-jsifonly * { box-sizing: border-box; }
.infographic-jsifonly .ig-inner { padding: 1.75rem clamp(1.25rem, 5vw, 3rem) 2rem; }
.infographic-jsifonly .ig-title {
font-family: var(--display-font); font-size: clamp(1.4rem, 4.5vw, 1.9rem);
font-weight: 600; text-align: center;
}
.infographic-jsifonly .controls { display: flex; flex-direction: column; align-items: center; gap: 0.6rem; }
.infographic-jsifonly .input-wrap { position: relative; }
.infographic-jsifonly .input-tag {
position: absolute; top: -1.35rem; left: 50%; transform: translateX(-50%);
font-size: 0.65rem; font-family: var(--mono-font); color: var(--accent);
background: var(--accent-soft); padding: 0.1rem 0.45rem; border-radius: 4px; white-space: nowrap;
}
.infographic-jsifonly .age-input {
width: 6rem; height: 2.6rem; font-family: var(--mono-font); font-size: 1.1rem; font-weight: 700;
text-align: center; padding: 0 0.4rem; border: 1px solid var(--line); background: var(--paper-alt);
color: var(--ink); outline: none;
}
.infographic-jsifonly .age-input:focus { border-color: var(--accent); }
.infographic-jsifonly .presets { display: flex; gap: 0.4rem; justify-content: center; width: 100%; }
.infographic-jsifonly .preset-btn {
font: inherit; font-size: 0.75rem; font-family: var(--mono-font); border: 1px dashed var(--line);
background: var(--paper); color: var(--ink-soft); padding: 0.3rem 0.65rem; border-radius: 4px; cursor: pointer;
}
.infographic-jsifonly .preset-btn:hover { border-color: var(--accent); color: var(--accent); }
.infographic-jsifonly .stage { display: flex; justify-content: center; }
.infographic-jsifonly .stage svg { width: 100%; max-width: 420px; height: auto; overflow: visible; }
.infographic-jsifonly .fc-shape { fill: var(--paper-alt); stroke: var(--line); stroke-width: 1.8; transition: fill 0.2s, stroke 0.2s; }
.infographic-jsifonly .fc-shape.reached-yes { fill: var(--accent-soft); stroke: var(--accent); filter: drop-shadow(0 3px 6px rgba(0,0,0,0.15)); }
.infographic-jsifonly .fc-shape.reached-no { fill: var(--paper-alt); stroke: var(--accent); }
.infographic-jsifonly .fc-block.reached { fill: var(--ok-soft); stroke: var(--ok); filter: drop-shadow(0 3px 6px rgba(0,0,0,0.15)); }
.infographic-jsifonly .fc-pill { fill: var(--accent-soft); stroke: var(--accent); }
.infographic-jsifonly .fc-text { font-family: var(--mono-font); font-size: 12px; fill: var(--ink); text-anchor: middle; }
.infographic-jsifonly .fc-text.on-pill { fill: var(--accent); font-weight: 700; }
.infographic-jsifonly .fc-text.on-block { font-weight: 700; }
.infographic-jsifonly .fc-mark { font-family: var(--mono-font); font-size: 13px; font-weight: 700; text-anchor: middle; }
.infographic-jsifonly .fc-mark.yes { fill: var(--accent); }
.infographic-jsifonly .fc-mark.no { fill: var(--ink-soft); }
.infographic-jsifonly .fc-edge path { stroke: var(--line); stroke-width: 2; fill: none; transition: stroke 0.2s; }
.infographic-jsifonly .fc-edge polygon { fill: var(--line); transition: fill 0.2s; }
.infographic-jsifonly .fc-edge.is-check path { stroke: var(--accent); stroke-width: 2.5; }
.infographic-jsifonly .fc-edge.is-check polygon { fill: var(--accent); }
.infographic-jsifonly .fc-edge.is-taken path { stroke: var(--ok); stroke-width: 2.5; }
.infographic-jsifonly .fc-edge.is-taken polygon { fill: var(--ok); }
.infographic-jsifonly .junction { fill: var(--line); transition: fill 0.2s; }
.infographic-jsifonly .junction.is-taken { fill: var(--ok); }
.infographic-jsifonly .caption {
text-align: center; font-size: 0.9rem; color: var(--ink-soft);
}
.infographic-jsifonly .caption strong { color: var(--ink); font-family: var(--mono-font); }
@media (prefers-reduced-motion: reduce) {
.infographic-jsifonly *, .infographic-jsifonly *::before, .infographic-jsifonly *::after {
animation-duration: 0.001ms !important; transition-duration: 0.001ms !important;
}
}
</style>
<div class="ig-inner">
<h2 class="ig-title">The if Statement</h2>
<div class="controls">
<div class="input-wrap">
<span class="input-tag">age</span>
<input type="number" class="age-input" id="ageInput" value="20" min="0" max="120">
</div>
<div class="presets">
<button class="preset-btn" data-v="25">25</button>
<button class="preset-btn" data-v="15">15</button>
</div>
</div>
<div class="stage">
<svg viewBox="0 0 480 470" xmlns="http://www.w3.org/2000/svg">
<!-- Start -->
<g id="n-start">
<rect class="fc-shape fc-pill" x="100" y="10" width="140" height="42" rx="21"></rect>
<text class="fc-text on-pill" x="170" y="36">Start</text>
</g>
<!-- Start -> Diamond -->
<g class="fc-edge" id="e-start-d">
<path d="M170,52 L170,79"></path>
<polygon points="170,85 165,75 175,75"></polygon>
</g>
<!-- Diamond -->
<g id="n-d">
<polygon class="fc-shape" points="170,85 250,140 170,195 90,140"></polygon>
<text class="fc-text" x="170" y="136">age >=</text>
<text class="fc-text" x="170" y="150">18 ?</text>
<text class="fc-mark" id="mark-d" x="204" y="105"></text>
</g>
<!-- Yes -> Block -->
<g class="fc-edge" id="e-d-yes">
<path d="M250,140 L263,140"></path>
<polygon points="270,140 260,135 260,145"></polygon>
</g>
<text class="fc-text" x="256" y="130" font-size="10" fill="var(--ink-soft)">yes</text>
<g id="n-block">
<rect class="fc-shape fc-block" x="270" y="115" width="120" height="50" rx="8"></rect>
<text class="fc-text on-block" x="330" y="141">Can vote</text>
<text class="fc-text" x="330" y="155" font-size="9" fill="var(--ink-soft)">(if block)</text>
</g>
<!-- No -> straight down, skipping the block -->
<g class="fc-edge" id="e-d-no">
<path d="M170,195 L170,334"></path>
</g>
<text class="fc-text" x="186" y="230" font-size="10" fill="var(--ink-soft)">no</text>
<!-- Block -> down -> left -> junction -->
<g class="fc-edge" id="e-block-merge">
<path d="M330,165 L330,340 L170,340"></path>
</g>
<!-- Junction dot -->
<circle class="junction" id="junction" cx="170" cy="340" r="4.5"></circle>
<!-- Junction -> End -->
<g class="fc-edge" id="e-merge-end">
<path d="M170,340 L170,384"></path>
<polygon points="170,390 165,380 175,380"></polygon>
</g>
<!-- End -->
<g id="n-end">
<rect class="fc-shape fc-pill" x="100" y="390" width="140" height="42" rx="21"></rect>
<text class="fc-text on-pill" x="170" y="416">End</text>
</g>
</svg>
</div>
<p class="caption" id="caption"></p>
</div>
</div>
<script>
(function () {
const wrapper = document.currentScript.closest('.infographic-jsifonly')
|| document.querySelector('.infographic-jsifonly');
const bodyAttr = document.body.getAttribute('data-darkmode');
let dark;
if (bodyAttr === 'true') {
dark = true;
} else if (bodyAttr === 'false') {
dark = false;
} else {
const match = document.cookie.match(/(?:^|;\s*)darkMode=([^;]+)/);
dark = match ? match[1] === 'true' : false;
}
if (dark) wrapper.setAttribute('data-theme', 'dark');
const input = wrapper.querySelector('#ageInput');
const caption = wrapper.querySelector('#caption');
const d = wrapper.querySelector('#n-d .fc-shape');
const mark = wrapper.querySelector('#mark-d');
const block = wrapper.querySelector('#n-block .fc-block');
const junction = wrapper.querySelector('#junction');
const eStartD = wrapper.querySelector('#e-start-d');
const eYes = wrapper.querySelector('#e-d-yes');
const eNo = wrapper.querySelector('#e-d-no');
const eBlockMerge = wrapper.querySelector('#e-block-merge');
const eMergeEnd = wrapper.querySelector('#e-merge-end');
const allEdges = [eStartD, eYes, eNo, eBlockMerge, eMergeEnd];
function reset() {
d.classList.remove('reached-yes', 'reached-no');
mark.textContent = ''; mark.classList.remove('yes', 'no');
block.classList.remove('reached');
junction.classList.remove('is-taken');
allEdges.forEach(e => e.classList.remove('is-check', 'is-taken'));
}
function update() {
reset();
const age = Number(input.value);
eStartD.classList.add('is-check');
if (age >= 18) {
d.classList.add('reached-yes');
mark.textContent = 'β'; mark.classList.add('yes');
eYes.classList.add('is-check');
block.classList.add('reached');
eBlockMerge.classList.add('is-taken');
eMergeEnd.classList.add('is-taken');
junction.classList.add('is-taken');
caption.innerHTML = `<strong>age = ${isNaN(age) ? '?' : age}</strong> β condition true, block runs`;
} else {
d.classList.add('reached-no');
mark.textContent = 'β'; mark.classList.add('no');
eNo.classList.add('is-taken');
eMergeEnd.classList.add('is-taken');
junction.classList.add('is-taken');
caption.innerHTML = `<strong>age = ${isNaN(age) ? '?' : age}</strong> β condition false, block skipped`;
}
}
input.addEventListener('input', update);
wrapper.querySelectorAll('.preset-btn').forEach(btn => {
btn.addEventListener('click', () => { input.value = btn.dataset.v; update(); });
});
update();
})();
</script>.demo {
font-family: system-ui, sans-serif;
padding: 16px;
}
.demo h1 {
color: #2563eb;
margin: 0 0 8px;
}
.demo p {
color: #4b5563;
margin: 0;
}document.querySelector('.demo h1')?.addEventListener('click', () => {
alert('Hello from live preview!');
});π§© The else Statement
Use the else statement to specify a block of code to be executed if the condition is false.
Syntax :
if (*condition*) {
// *block of code to be executed if the condition is true*
} else {
// *block of code to be executed if the condition is false*
}Example :
If the hour is less than 18, create a "Good day" greeting, otherwise "Good evening" :
let time = 21;
let greeting;
if (time < 20) {
greeting = "Good morning";
} else {
greeting = "Good evening";
}
console.log(greeting);Good evening<div class="infographic-jsifelse">
<style>
.infographic-jsifelse {
--ink: #1a1a1a;
--ink-soft: #5c5c5c;
--paper: #ffffff;
--paper-alt: #f0ece4;
--accent: #2563eb;
--accent-soft:#e3ebfd;
--line: #ddd6c9;
--ok: #1a7f4e;
--ok-soft: #e3f5ec;
--display-font: 'Fraunces', Georgia, serif;
--body-font: 'Inter', -apple-system, sans-serif;
--mono-font: 'IBM Plex Mono', 'Courier New', monospace;
max-width: 620px;
margin: 0 auto;
box-sizing: border-box;
font-family: var(--body-font);
color: var(--ink);
background: var(--paper);
}
.infographic-jsifelse[data-theme="dark"] {
--ink: #e8e6e1;
--ink-soft: #9a968c;
--paper: #121212;
--paper-alt: #1e1e26;
--accent: #5b8def;
--accent-soft:#202942;
--line: #2c313d;
--ok: #5cd6a0;
--ok-soft: #16342a;
}
.infographic-jsifelse * { box-sizing: border-box; }
.infographic-jsifelse .ig-inner { padding: 1.75rem clamp(1.25rem, 5vw, 3rem) 2rem; }
.infographic-jsifelse .ig-title {
font-family: var(--display-font); font-size: clamp(1.4rem, 4.5vw, 1.9rem);
font-weight: 600; text-align: center; margin: 0 0 2.25rem;
}
.infographic-jsifelse .controls { display: flex; flex-direction: column; align-items: center; gap: 0.6rem; }
.infographic-jsifelse .input-wrap { position: relative; }
.infographic-jsifelse .input-tag {
position: absolute; top: -1.35rem; left: 50%; transform: translateX(-50%);
font-size: 0.65rem; font-family: var(--mono-font); color: var(--accent);
background: var(--accent-soft); padding: 0.1rem 0.45rem; border-radius: 4px; white-space: nowrap;
}
.infographic-jsifelse .age-input {
width: 6rem; height: 2.6rem; font-family: var(--mono-font); font-size: 1.1rem; font-weight: 700;
text-align: center; padding: 0 0.4rem; border: 1px solid var(--line); background: var(--paper-alt);
color: var(--ink); outline: none;
}
.infographic-jsifelse .age-input:focus { border-color: var(--accent); }
.infographic-jsifelse .presets { display: flex; gap: 0.4rem; justify-content: center; margin-top: 1.6rem; width: 100%; }
.infographic-jsifelse .preset-btn {
font: inherit; font-size: 0.75rem; font-family: var(--mono-font); border: 1px dashed var(--line);
background: var(--paper); color: var(--ink-soft); padding: 0.3rem 0.65rem; border-radius: 4px; cursor: pointer;
}
.infographic-jsifelse .preset-btn:hover { border-color: var(--accent); color: var(--accent); }
.infographic-jsifelse .stage { margin-top: 1.5rem; display: flex; justify-content: center; }
.infographic-jsifelse .stage svg { width: 100%; max-width: 500px; height: auto; overflow: visible; }
.infographic-jsifelse .fc-shape { fill: var(--paper-alt); stroke: var(--line); stroke-width: 1.8; transition: fill 0.2s, stroke 0.2s; }
.infographic-jsifelse .fc-shape.reached-yes { fill: var(--accent-soft); stroke: var(--accent); filter: drop-shadow(0 3px 6px rgba(0,0,0,0.15)); }
.infographic-jsifelse .fc-shape.reached-no { fill: var(--paper-alt); stroke: var(--accent); }
.infographic-jsifelse .fc-block.reached { fill: var(--ok-soft); stroke: var(--ok); filter: drop-shadow(0 3px 6px rgba(0,0,0,0.15)); }
.infographic-jsifelse .fc-pill { fill: var(--accent-soft); stroke: var(--accent); }
.infographic-jsifelse .fc-text { font-family: var(--mono-font); font-size: 12px; fill: var(--ink); text-anchor: middle; }
.infographic-jsifelse .fc-text.on-pill { fill: var(--accent); font-weight: 700; }
.infographic-jsifelse .fc-text.on-block { font-weight: 700; }
.infographic-jsifelse .fc-mark { font-family: var(--mono-font); font-size: 13px; font-weight: 700; text-anchor: middle; }
.infographic-jsifelse .fc-mark.yes { fill: var(--accent); }
.infographic-jsifelse .fc-mark.no { fill: var(--ink-soft); }
.infographic-jsifelse .fc-edge path { stroke: var(--line); stroke-width: 2; fill: none; transition: stroke 0.2s; }
.infographic-jsifelse .fc-edge polygon { fill: var(--line); transition: fill 0.2s; }
.infographic-jsifelse .fc-edge.is-check path { stroke: var(--accent); stroke-width: 2.5; }
.infographic-jsifelse .fc-edge.is-check polygon { fill: var(--accent); }
.infographic-jsifelse .fc-edge.is-taken path { stroke: var(--ok); stroke-width: 2.5; }
.infographic-jsifelse .fc-edge.is-taken polygon { fill: var(--ok); }
.infographic-jsifelse .junction { fill: var(--line); transition: fill 0.2s; }
.infographic-jsifelse .junction.is-taken { fill: var(--ok); }
.infographic-jsifelse .caption {
text-align: center; font-size: 0.9rem; color: var(--ink-soft); margin-top: 1.25rem;
}
.infographic-jsifelse .caption strong { color: var(--ink); font-family: var(--mono-font); }
@media (prefers-reduced-motion: reduce) {
.infographic-jsifelse *, .infographic-jsifelse *::before, .infographic-jsifelse *::after {
animation-duration: 0.001ms !important; transition-duration: 0.001ms !important;
}
}
</style>
<div class="ig-inner">
<h2 class="ig-title">The if...else Statement</h2>
<div class="controls">
<div class="input-wrap">
<span class="input-tag">age</span>
<input type="number" class="age-input" id="ageInput" value="20" min="0" max="120">
</div>
<div class="presets">
<button class="preset-btn" data-v="25">25</button>
<button class="preset-btn" data-v="15">15</button>
</div>
</div>
<div class="stage">
<svg viewBox="0 0 560 470" xmlns="http://www.w3.org/2000/svg">
<!-- Start -->
<g id="n-start">
<rect class="fc-shape fc-pill" x="210" y="10" width="140" height="42" rx="21"></rect>
<text class="fc-text on-pill" x="280" y="36">Start</text>
</g>
<!-- Start -> Diamond -->
<g class="fc-edge" id="e-start-d">
<path d="M280,52 L280,79"></path>
<polygon points="280,85 275,75 285,75"></polygon>
</g>
<!-- Diamond -->
<g id="n-d">
<polygon class="fc-shape" points="280,85 360,140 280,195 200,140"></polygon>
<text class="fc-text" x="280" y="136">age >=</text>
<text class="fc-text" x="280" y="150">18 ?</text>
<text class="fc-mark" id="mark-d" x="314" y="105"></text>
</g>
<!-- Yes -> right block -->
<g class="fc-edge" id="e-d-yes">
<path d="M360,140 L373,140"></path>
<polygon points="380,140 370,135 370,145"></polygon>
</g>
<text class="fc-text" x="366" y="130" font-size="10" fill="var(--ink-soft)">yes</text>
<g id="n-blockYes">
<rect class="fc-shape fc-block" x="380" y="115" width="130" height="50" rx="8"></rect>
<text class="fc-text on-block" x="445" y="141">Can vote</text>
<text class="fc-text" x="445" y="155" font-size="9" fill="var(--ink-soft)">(if block)</text>
</g>
<!-- No -> left block -->
<g class="fc-edge" id="e-d-no">
<path d="M200,140 L187,140"></path>
<polygon points="180,140 190,135 190,145"></polygon>
</g>
<text class="fc-text" x="194" y="130" font-size="10" fill="var(--ink-soft)">no</text>
<g id="n-blockNo">
<rect class="fc-shape fc-block" x="50" y="115" width="130" height="50" rx="8"></rect>
<text class="fc-text on-block" x="115" y="141">Cannot vote</text>
<text class="fc-text" x="115" y="155" font-size="9" fill="var(--ink-soft)">(else block)</text>
</g>
<!-- Yes block -> down -> left -> junction -->
<g class="fc-edge" id="e-yes-merge">
<path d="M445,165 L445,340 L280,340"></path>
</g>
<!-- No block -> down -> right -> junction -->
<g class="fc-edge" id="e-no-merge">
<path d="M115,165 L115,340 L280,340"></path>
</g>
<!-- Junction dot -->
<circle class="junction" id="junction" cx="280" cy="340" r="4.5"></circle>
<!-- Junction -> End -->
<g class="fc-edge" id="e-merge-end">
<path d="M280,340 L280,384"></path>
<polygon points="280,390 275,380 285,380"></polygon>
</g>
<!-- End -->
<g id="n-end">
<rect class="fc-shape fc-pill" x="210" y="390" width="140" height="42" rx="21"></rect>
<text class="fc-text on-pill" x="280" y="416">End</text>
</g>
</svg>
</div>
<p class="caption" id="caption"></p>
</div>
</div>
<script>
(function () {
const wrapper = document.currentScript.closest('.infographic-jsifelse')
|| document.querySelector('.infographic-jsifelse');
const bodyAttr = document.body.getAttribute('data-darkmode');
let dark;
if (bodyAttr === 'true') {
dark = true;
} else if (bodyAttr === 'false') {
dark = false;
} else {
const match = document.cookie.match(/(?:^|;\s*)darkMode=([^;]+)/);
dark = match ? match[1] === 'true' : false;
}
if (dark) wrapper.setAttribute('data-theme', 'dark');
const input = wrapper.querySelector('#ageInput');
const caption = wrapper.querySelector('#caption');
const d = wrapper.querySelector('#n-d .fc-shape');
const mark = wrapper.querySelector('#mark-d');
const blockYes = wrapper.querySelector('#n-blockYes .fc-block');
const blockNo = wrapper.querySelector('#n-blockNo .fc-block');
const junction = wrapper.querySelector('#junction');
const eStartD = wrapper.querySelector('#e-start-d');
const eYes = wrapper.querySelector('#e-d-yes');
const eNo = wrapper.querySelector('#e-d-no');
const eYesMerge = wrapper.querySelector('#e-yes-merge');
const eNoMerge = wrapper.querySelector('#e-no-merge');
const eMergeEnd = wrapper.querySelector('#e-merge-end');
const allEdges = [eStartD, eYes, eNo, eYesMerge, eNoMerge, eMergeEnd];
function reset() {
d.classList.remove('reached-yes', 'reached-no');
mark.textContent = ''; mark.classList.remove('yes', 'no');
blockYes.classList.remove('reached');
blockNo.classList.remove('reached');
junction.classList.remove('is-taken');
allEdges.forEach(e => e.classList.remove('is-check', 'is-taken'));
}
function update() {
reset();
const age = Number(input.value);
eStartD.classList.add('is-check');
if (age >= 18) {
d.classList.add('reached-yes');
mark.textContent = 'β'; mark.classList.add('yes');
eYes.classList.add('is-check');
blockYes.classList.add('reached');
eYesMerge.classList.add('is-taken');
eMergeEnd.classList.add('is-taken');
junction.classList.add('is-taken');
caption.innerHTML = `<strong>age = ${isNaN(age) ? '?' : age}</strong> β condition true, if block runs`;
} else {
d.classList.add('reached-no');
mark.textContent = 'β'; mark.classList.add('no');
eNo.classList.add('is-check');
blockNo.classList.add('reached');
eNoMerge.classList.add('is-taken');
eMergeEnd.classList.add('is-taken');
junction.classList.add('is-taken');
caption.innerHTML = `<strong>age = ${isNaN(age) ? '?' : age}</strong> β condition false, else block runs`;
}
}
input.addEventListener('input', update);
wrapper.querySelectorAll('.preset-btn').forEach(btn => {
btn.addEventListener('click', () => { input.value = btn.dataset.v; update(); });
});
update();
})();
</script>.demo {
font-family: system-ui, sans-serif;
padding: 16px;
}
.demo h1 {
color: #2563eb;
margin: 0 0 8px;
}
.demo p {
color: #4b5563;
margin: 0;
}document.querySelector('.demo h1')?.addEventListener('click', () => {
alert('Hello from live preview!');
});π The else if Statement
Use the else if statement to specify a new condition if the first condition is false.
Syntax :
if (*condition1*) {
// *block of code to be executed if condition1 is true*
} else if (*condition2*) {
// *block of code to be executed if the condition1 is false and condition2 is true*
} else {
// *block of code to be executed if the condition1 is false and condition2 is false*
}Example :
let time = 14;
let greeting;
if (time < 10) {
greeting = "Good morning";
} else if (time < 20) {
greeting = "Good day";
} else {
greeting = "Good evening";
}
console.log(greeting);Good day<div class="infographic-jsifei">
<style>
.infographic-jsifei {
--ink: #1a1a1a;
--ink-soft: #5c5c5c;
--paper: #ffffff;
--paper-alt: #f0ece4;
--accent: #2563eb;
--accent-soft:#e3ebfd;
--line: #ddd6c9;
--ok: #1a7f4e;
--ok-soft: #e3f5ec;
--display-font: 'Fraunces', Georgia, serif;
--body-font: 'Inter', -apple-system, sans-serif;
--mono-font: 'IBM Plex Mono', 'Courier New', monospace;
max-width: 620px;
margin: 0 auto;
box-sizing: border-box;
font-family: var(--body-font);
color: var(--ink);
background: var(--paper);
}
.infographic-jsifei[data-theme="dark"] {
--ink: #e8e6e1;
--ink-soft: #9a968c;
--paper: #121212;
--paper-alt: #1e1e26;
--accent: #5b8def;
--accent-soft:#202942;
--line: #2c313d;
--ok: #5cd6a0;
--ok-soft: #16342a;
}
.infographic-jsifei * { box-sizing: border-box; }
.infographic-jsifei .ig-inner { padding: 1.75rem clamp(1.25rem, 5vw, 3rem) 2rem; }
.infographic-jsifei .ig-title {
font-family: var(--display-font); font-size: clamp(1.4rem, 4.5vw, 1.9rem);
font-weight: 600; text-align: center; margin: 0 0 2.25rem;
}
.infographic-jsifei .controls { display: flex; flex-direction: column; align-items: center; gap: 0.6rem; }
.infographic-jsifei .input-wrap { position: relative; }
.infographic-jsifei .input-tag {
position: absolute; top: -1.35rem; left: 50%; transform: translateX(-50%);
font-size: 0.65rem; font-family: var(--mono-font); color: var(--accent);
background: var(--accent-soft); padding: 0.1rem 0.45rem; border-radius: 4px; white-space: nowrap;
}
.infographic-jsifei .score-input {
width: 6rem; height: 2.6rem; font-family: var(--mono-font); font-size: 1.1rem; font-weight: 700;
text-align: center; padding: 0 0.4rem; border: 1px solid var(--line); background: var(--paper-alt);
color: var(--ink); outline: none;
}
.infographic-jsifei .score-input:focus { border-color: var(--accent); }
.infographic-jsifei .presets { display: flex; gap: 0.4rem; justify-content: center; margin-top: 1.6rem; width: 100%; }
.infographic-jsifei .preset-btn {
font: inherit; font-size: 0.75rem; font-family: var(--mono-font); border: 1px dashed var(--line);
background: var(--paper); color: var(--ink-soft); padding: 0.3rem 0.65rem; border-radius: 4px; cursor: pointer;
}
.infographic-jsifei .preset-btn:hover { border-color: var(--accent); color: var(--accent); }
.infographic-jsifei .stage { margin-top: 1.5rem; display: flex; justify-content: center; }
.infographic-jsifei .stage svg { width: 100%; max-width: 500px; height: auto; overflow: visible; }
.infographic-jsifei .fc-shape { fill: var(--paper-alt); stroke: var(--line); stroke-width: 1.8; transition: fill 0.2s, stroke 0.2s; }
.infographic-jsifei .fc-shape.reached-yes { fill: var(--accent-soft); stroke: var(--accent); filter: drop-shadow(0 3px 6px rgba(0,0,0,0.15)); }
.infographic-jsifei .fc-shape.reached-no { fill: var(--paper-alt); stroke: var(--accent); }
.infographic-jsifei .fc-block.reached { fill: var(--ok-soft); stroke: var(--ok); filter: drop-shadow(0 3px 6px rgba(0,0,0,0.15)); }
.infographic-jsifei .fc-pill { fill: var(--accent-soft); stroke: var(--accent); }
.infographic-jsifei .fc-text { font-family: var(--mono-font); font-size: 12px; fill: var(--ink); text-anchor: middle; }
.infographic-jsifei .fc-text.on-pill { fill: var(--accent); font-weight: 700; }
.infographic-jsifei .fc-text.on-block { font-weight: 700; }
.infographic-jsifei .fc-mark { font-family: var(--mono-font); font-size: 13px; font-weight: 700; text-anchor: middle; }
.infographic-jsifei .fc-mark.yes { fill: var(--accent); }
.infographic-jsifei .fc-mark.no { fill: var(--ink-soft); }
.infographic-jsifei .fc-edge path { stroke: var(--line); stroke-width: 2; fill: none; transition: stroke 0.2s; }
.infographic-jsifei .fc-edge polygon { fill: var(--line); transition: fill 0.2s; }
.infographic-jsifei .fc-edge.is-check path { stroke: var(--accent); stroke-width: 2.5; }
.infographic-jsifei .fc-edge.is-check polygon { fill: var(--accent); }
.infographic-jsifei .fc-edge.is-taken path { stroke: var(--ok); stroke-width: 2.5; }
.infographic-jsifei .fc-edge.is-taken polygon { fill: var(--ok); }
.infographic-jsifei .junction { fill: var(--line); transition: fill 0.2s; }
.infographic-jsifei .junction.is-taken { fill: var(--ok); }
.infographic-jsifei .caption {
text-align: center; font-size: 0.9rem; color: var(--ink-soft); margin-top: 1.25rem;
}
.infographic-jsifei .caption strong { color: var(--ink); font-family: var(--mono-font); }
@media (prefers-reduced-motion: reduce) {
.infographic-jsifei *, .infographic-jsifei *::before, .infographic-jsifei *::after {
animation-duration: 0.001ms !important; transition-duration: 0.001ms !important;
}
}
</style>
<div class="ig-inner">
<h2 class="ig-title">The if...else if...else Statement</h2>
<div class="controls">
<div class="input-wrap">
<span class="input-tag">score</span>
<input type="number" class="score-input" id="scoreInput" value="75" min="0" max="100">
</div>
<div class="presets">
<button class="preset-btn" data-v="95">95</button>
<button class="preset-btn" data-v="72">72</button>
<button class="preset-btn" data-v="45">45</button>
</div>
</div>
<div class="stage">
<svg viewBox="0 0 560 670" xmlns="http://www.w3.org/2000/svg">
<!-- Start -->
<g id="n-start">
<rect class="fc-shape fc-pill" x="135" y="10" width="140" height="42" rx="21"></rect>
<text class="fc-text on-pill" x="205" y="36">Start</text>
</g>
<!-- Start -> Diamond 1 -->
<g class="fc-edge" id="e-start-d1">
<path d="M205,52 L205,79"></path>
<polygon points="205,85 200,75 210,75"></polygon>
</g>
<!-- Diamond 1 -->
<g id="n-d1">
<polygon class="fc-shape" points="205,85 285,140 205,195 125,140"></polygon>
<text class="fc-text" x="205" y="136">score</text>
<text class="fc-text" x="205" y="150">>= 90 ?</text>
<text class="fc-mark" id="mark-d1" x="239" y="105"></text>
</g>
<!-- Yes1 -> Block A -->
<g class="fc-edge" id="e-d1-yes">
<path d="M285,140 L298,140"></path>
<polygon points="305,140 295,135 295,145"></polygon>
</g>
<text class="fc-text" x="291" y="130" font-size="10" fill="var(--ink-soft)">yes</text>
<g id="n-blockA">
<rect class="fc-shape fc-block" x="305" y="115" width="130" height="50" rx="8"></rect>
<text class="fc-text on-block" x="370" y="146">Grade A</text>
</g>
<!-- No1 -> Diamond 2 -->
<g class="fc-edge" id="e-d1-no">
<path d="M205,195 L205,269"></path>
<polygon points="205,275 200,265 210,265"></polygon>
</g>
<text class="fc-text" x="221" y="240" font-size="10" fill="var(--ink-soft)">no</text>
<!-- Diamond 2 -->
<g id="n-d2">
<polygon class="fc-shape" points="205,275 285,330 205,385 125,330"></polygon>
<text class="fc-text" x="205" y="326">score</text>
<text class="fc-text" x="205" y="340">>= 60 ?</text>
<text class="fc-mark" id="mark-d2" x="239" y="295"></text>
</g>
<!-- Yes2 -> Block C -->
<g class="fc-edge" id="e-d2-yes">
<path d="M285,330 L298,330"></path>
<polygon points="305,330 295,325 295,335"></polygon>
</g>
<text class="fc-text" x="291" y="320" font-size="10" fill="var(--ink-soft)">yes</text>
<g id="n-blockC">
<rect class="fc-shape fc-block" x="305" y="305" width="130" height="50" rx="8"></rect>
<text class="fc-text on-block" x="370" y="336">Grade C</text>
</g>
<!-- No2 -> Block F (else) -->
<g class="fc-edge" id="e-d2-no">
<path d="M205,385 L205,439"></path>
<polygon points="205,445 200,435 210,435"></polygon>
</g>
<text class="fc-text" x="221" y="415" font-size="10" fill="var(--ink-soft)">no</text>
<g id="n-blockF">
<rect class="fc-shape fc-block" x="130" y="450" width="150" height="50" rx="8"></rect>
<text class="fc-text on-block" x="205" y="476">Grade F</text>
<text class="fc-text" x="205" y="490" font-size="9" fill="var(--ink-soft)">(else)</text>
</g>
<!-- Block A -> down and around Block C -> left -> junction -->
<g class="fc-edge" id="e-blockA-merge">
<path d="M370,165 L370,180 L460,180 L460,560 L205,560"></path>
</g>
<!-- Block C -> down -> left -> junction -->
<g class="fc-edge" id="e-blockC-merge">
<path d="M370,355 L370,560 L205,560"></path>
</g>
<!-- Block F -> straight down to junction -->
<g class="fc-edge" id="e-blockF-merge">
<path d="M205,500 L205,560"></path>
</g>
<!-- Junction dot -->
<circle class="junction" id="junction" cx="205" cy="560" r="4.5"></circle>
<!-- Junction -> End -->
<g class="fc-edge" id="e-merge-end">
<path d="M205,560 L205,604"></path>
<polygon points="205,610 200,600 210,600"></polygon>
</g>
<!-- End -->
<g id="n-end">
<rect class="fc-shape fc-pill" x="135" y="610" width="140" height="42" rx="21"></rect>
<text class="fc-text on-pill" x="205" y="636">End</text>
</g>
</svg>
</div>
<p class="caption" id="caption"></p>
</div>
</div>
<script>
(function () {
const wrapper = document.currentScript.closest('.infographic-jsifei')
|| document.querySelector('.infographic-jsifei');
const bodyAttr = document.body.getAttribute('data-darkmode');
let dark;
if (bodyAttr === 'true') {
dark = true;
} else if (bodyAttr === 'false') {
dark = false;
} else {
const match = document.cookie.match(/(?:^|;\s*)darkMode=([^;]+)/);
dark = match ? match[1] === 'true' : false;
}
if (dark) wrapper.setAttribute('data-theme', 'dark');
const input = wrapper.querySelector('#scoreInput');
const caption = wrapper.querySelector('#caption');
const d1 = wrapper.querySelector('#n-d1 .fc-shape');
const d2 = wrapper.querySelector('#n-d2 .fc-shape');
const markD1 = wrapper.querySelector('#mark-d1');
const markD2 = wrapper.querySelector('#mark-d2');
const blockA = wrapper.querySelector('#n-blockA .fc-block');
const blockC = wrapper.querySelector('#n-blockC .fc-block');
const blockF = wrapper.querySelector('#n-blockF .fc-block');
const junction = wrapper.querySelector('#junction');
const eStartD1 = wrapper.querySelector('#e-start-d1');
const eD1Yes = wrapper.querySelector('#e-d1-yes');
const eD1No = wrapper.querySelector('#e-d1-no');
const eD2Yes = wrapper.querySelector('#e-d2-yes');
const eD2No = wrapper.querySelector('#e-d2-no');
const eAMerge = wrapper.querySelector('#e-blockA-merge');
const eCMerge = wrapper.querySelector('#e-blockC-merge');
const eFMerge = wrapper.querySelector('#e-blockF-merge');
const eMergeEnd = wrapper.querySelector('#e-merge-end');
const allEdges = [eStartD1, eD1Yes, eD1No, eD2Yes, eD2No, eAMerge, eCMerge, eFMerge, eMergeEnd];
function reset() {
d1.classList.remove('reached-yes', 'reached-no');
d2.classList.remove('reached-yes', 'reached-no');
markD1.textContent = ''; markD1.classList.remove('yes', 'no');
markD2.textContent = ''; markD2.classList.remove('yes', 'no');
[blockA, blockC, blockF].forEach(b => b.classList.remove('reached'));
junction.classList.remove('is-taken');
allEdges.forEach(e => e.classList.remove('is-check', 'is-taken'));
}
function update() {
reset();
const score = Number(input.value);
eStartD1.classList.add('is-check');
let grade;
if (score >= 90) {
d1.classList.add('reached-yes');
markD1.textContent = 'β'; markD1.classList.add('yes');
eD1Yes.classList.add('is-check');
blockA.classList.add('reached');
eAMerge.classList.add('is-taken');
grade = 'A';
} else {
d1.classList.add('reached-no');
markD1.textContent = 'β'; markD1.classList.add('no');
eD1No.classList.add('is-check');
if (score >= 60) {
d2.classList.add('reached-yes');
markD2.textContent = 'β'; markD2.classList.add('yes');
eD2Yes.classList.add('is-check');
blockC.classList.add('reached');
eCMerge.classList.add('is-taken');
grade = 'C';
} else {
d2.classList.add('reached-no');
markD2.textContent = 'β'; markD2.classList.add('no');
eD2No.classList.add('is-check');
blockF.classList.add('reached');
eFMerge.classList.add('is-taken');
grade = 'F';
}
}
eMergeEnd.classList.add('is-taken');
junction.classList.add('is-taken');
caption.innerHTML = `<strong>score = ${isNaN(score) ? '?' : score}</strong> β Grade ${grade}`;
}
input.addEventListener('input', update);
wrapper.querySelectorAll('.preset-btn').forEach(btn => {
btn.addEventListener('click', () => { input.value = btn.dataset.v; update(); });
});
update();
})();
</script>.demo {
font-family: system-ui, sans-serif;
padding: 16px;
}
.demo h1 {
color: #2563eb;
margin: 0 0 8px;
}
.demo p {
color: #4b5563;
margin: 0;
}document.querySelector('.demo h1')?.addEventListener('click', () => {
alert('Hello from live preview!');
});π The Switch Statement
The switch statement is used to perform different actions based on different conditions, just like a board switch in real life:
π§ "If I press button 1, turn on light. If I press button 2, turn on fan..."
It's an alternative to multiple if...else if conditions β and is easier to read when you have many options.
This is how it works:
The switch expression is evaluated once.
The value of the expression is compared with the values of each case.
If there is a match, the associated block of code is executed.
If there is no match, the default code block is executed.
Syntax :
switch(*expression*) {
case *x*:
*// code block*
break;
case *y*:
*// code block*
break;
default:
// *code block*
}Example :
let day = 2;
let message;
switch(day) {
case 1:
message = "Monday";
break;
case 2:
message = "Tuesday";
break;
case 3:
message = "Wednesday";
break;
default:
message = "Another day";
}
console.log(message);Tuesday<div class="infographic-jsswitch">
<style>
.infographic-jsswitch {
--ink: #1a1a1a;
--ink-soft: #5c5c5c;
--paper: #ffffff;
--paper-alt: #f0ece4;
--accent: #2563eb;
--accent-soft:#e3ebfd;
--line: #ddd6c9;
--ok: #1a7f4e;
--ok-soft: #e3f5ec;
--display-font: 'Fraunces', Georgia, serif;
--body-font: 'Inter', -apple-system, sans-serif;
--mono-font: 'IBM Plex Mono', 'Courier New', monospace;
max-width: 680px;
margin: 0 auto;
box-sizing: border-box;
font-family: var(--body-font);
color: var(--ink);
background: var(--paper);
}
.infographic-jsswitch[data-theme="dark"] {
--ink: #e8e6e1;
--ink-soft: #9a968c;
--paper: #121212;
--paper-alt: #1e1e26;
--accent: #5b8def;
--accent-soft:#202942;
--line: #2c313d;
--ok: #5cd6a0;
--ok-soft: #16342a;
}
.infographic-jsswitch * { box-sizing: border-box; }
.infographic-jsswitch .ig-inner { padding: 1.75rem clamp(1.25rem, 5vw, 3rem) 2rem; }
.infographic-jsswitch .ig-title {
font-family: var(--display-font); font-size: clamp(1.4rem, 4.5vw, 1.9rem);
font-weight: 600; text-align: center; margin: 0 0 2.25rem;
}
.infographic-jsswitch .controls { display: flex; flex-direction: column; align-items: center; gap: 0.6rem; }
.infographic-jsswitch .input-wrap { position: relative; }
.infographic-jsswitch .input-tag {
position: absolute; top: -1.35rem; left: 50%; transform: translateX(-50%);
font-size: 0.65rem; font-family: var(--mono-font); color: var(--accent);
background: var(--accent-soft); padding: 0.1rem 0.45rem; border-radius: 4px; white-space: nowrap;
}
.infographic-jsswitch .fruit-input {
width: 9rem; height: 2.6rem; font-family: var(--mono-font); font-size: 1rem; font-weight: 700;
text-align: center; padding: 0 0.4rem; border: 1px solid var(--line); background: var(--paper-alt);
color: var(--ink); outline: none;
}
.infographic-jsswitch .fruit-input:focus { border-color: var(--accent); }
.infographic-jsswitch .presets { display: flex; gap: 0.4rem; justify-content: center; margin-top: 1.6rem; width: 100%; flex-wrap: wrap; }
.infographic-jsswitch .preset-btn {
font: inherit; font-size: 0.75rem; font-family: var(--mono-font); border: 1px dashed var(--line);
background: var(--paper); color: var(--ink-soft); padding: 0.3rem 0.65rem; border-radius: 4px; cursor: pointer;
}
.infographic-jsswitch .preset-btn:hover { border-color: var(--accent); color: var(--accent); }
.infographic-jsswitch .stage { margin-top: 1.5rem; display: flex; justify-content: center; }
.infographic-jsswitch .stage svg { width: 100%; max-width: 560px; height: auto; overflow: visible; }
.infographic-jsswitch .fc-shape { fill: var(--paper-alt); stroke: var(--line); stroke-width: 1.8; transition: fill 0.2s, stroke 0.2s; }
.infographic-jsswitch .fc-block.reached { fill: var(--ok-soft); stroke: var(--ok); filter: drop-shadow(0 3px 6px rgba(0,0,0,0.15)); }
.infographic-jsswitch .fc-pill { fill: var(--accent-soft); stroke: var(--accent); }
.infographic-jsswitch .fc-switch { fill: var(--accent-soft); stroke: var(--accent); stroke-width: 2; }
.infographic-jsswitch .fc-text { font-family: var(--mono-font); font-size: 12px; fill: var(--ink); text-anchor: middle; }
.infographic-jsswitch .fc-text.on-pill { fill: var(--accent); font-weight: 700; }
.infographic-jsswitch .fc-text.on-switch { fill: var(--accent); font-weight: 700; }
.infographic-jsswitch .fc-text.on-block { font-weight: 700; }
.infographic-jsswitch .case-tag {
font-family: var(--mono-font); font-size: 9.5px; fill: var(--ink-soft); text-anchor: middle;
}
.infographic-jsswitch .fc-edge path { stroke: var(--line); stroke-width: 2; fill: none; transition: stroke 0.2s; }
.infographic-jsswitch .fc-edge polygon { fill: var(--line); transition: fill 0.2s; }
.infographic-jsswitch .fc-edge.is-check path { stroke: var(--accent); stroke-width: 2.5; }
.infographic-jsswitch .fc-edge.is-check polygon { fill: var(--accent); }
.infographic-jsswitch .fc-edge.is-taken path { stroke: var(--ok); stroke-width: 2.5; }
.infographic-jsswitch .fc-edge.is-taken polygon { fill: var(--ok); }
.infographic-jsswitch .junction { fill: var(--line); transition: fill 0.2s; }
.infographic-jsswitch .junction.is-taken { fill: var(--ok); }
.infographic-jsswitch .caption {
text-align: center; font-size: 0.9rem; color: var(--ink-soft); margin-top: 1.25rem;
}
.infographic-jsswitch .caption strong { color: var(--ink); font-family: var(--mono-font); }
@media (prefers-reduced-motion: reduce) {
.infographic-jsswitch *, .infographic-jsswitch *::before, .infographic-jsswitch *::after {
animation-duration: 0.001ms !important; transition-duration: 0.001ms !important;
}
}
</style>
<div class="ig-inner">
<h2 class="ig-title">The switch Statement</h2>
<div class="controls">
<div class="input-wrap">
<span class="input-tag">fruit</span>
<input type="text" class="fruit-input" id="fruitInput" value="apple" autocomplete="off" spellcheck="false">
</div>
<div class="presets">
<button class="preset-btn" data-v="apple">apple</button>
<button class="preset-btn" data-v="banana">banana</button>
<button class="preset-btn" data-v="orange">orange</button>
<button class="preset-btn" data-v="kiwi">kiwi</button>
</div>
</div>
<div class="stage">
<svg viewBox="0 0 620 500" xmlns="http://www.w3.org/2000/svg">
<!-- Start -->
<g id="n-start">
<rect class="fc-shape fc-pill" x="240" y="10" width="140" height="42" rx="21"></rect>
<text class="fc-text on-pill" x="310" y="36">Start</text>
</g>
<!-- Start -> Switch -->
<g class="fc-edge" id="e-start-sw">
<path d="M310,52 L310,89"></path>
<polygon points="310,95 305,85 315,85"></polygon>
</g>
<!-- Switch node -->
<g id="n-switch">
<rect class="fc-switch" x="230" y="95" width="160" height="50" rx="6"></rect>
<line x1="238" y1="95" x2="238" y2="145" stroke="var(--accent)" stroke-width="2"></line>
<line x1="382" y1="95" x2="382" y2="145" stroke="var(--accent)" stroke-width="2"></line>
<text class="fc-text on-switch" x="310" y="124">switch (fruit)</text>
</g>
<!-- Fan-out lines from switch to each case block -->
<g class="fc-edge" id="e-sw-b1"><path d="M270,145 L85,225"></path></g>
<g class="fc-edge" id="e-sw-b2"><path d="M300,145 L235,225"></path></g>
<g class="fc-edge" id="e-sw-b3"><path d="M320,145 L385,225"></path></g>
<g class="fc-edge" id="e-sw-b4"><path d="M350,145 L535,225"></path></g>
<!-- Case tags -->
<text class="case-tag" x="85" y="215">case 'apple'</text>
<text class="case-tag" x="235" y="215">case 'banana'</text>
<text class="case-tag" x="385" y="215">case 'orange'</text>
<text class="case-tag" x="535" y="215">default</text>
<!-- Blocks -->
<g id="n-block1">
<rect class="fc-shape fc-block" x="25" y="225" width="120" height="50" rx="8"></rect>
<text class="fc-text on-block" x="85" y="255">π Apple</text>
</g>
<g id="n-block2">
<rect class="fc-shape fc-block" x="175" y="225" width="120" height="50" rx="8"></rect>
<text class="fc-text on-block" x="235" y="255">π Banana</text>
</g>
<g id="n-block3">
<rect class="fc-shape fc-block" x="325" y="225" width="120" height="50" rx="8"></rect>
<text class="fc-text on-block" x="385" y="255">π Orange</text>
</g>
<g id="n-block4">
<rect class="fc-shape fc-block" x="475" y="225" width="120" height="50" rx="8"></rect>
<text class="fc-text on-block" x="535" y="255">β Unknown</text>
</g>
<!-- Merge lines -->
<g class="fc-edge" id="e-b1-merge"><path d="M85,275 L85,380 L310,380"></path></g>
<g class="fc-edge" id="e-b2-merge"><path d="M235,275 L235,380 L310,380"></path></g>
<g class="fc-edge" id="e-b3-merge"><path d="M385,275 L385,380 L310,380"></path></g>
<g class="fc-edge" id="e-b4-merge"><path d="M535,275 L535,380 L310,380"></path></g>
<!-- Junction -->
<circle class="junction" id="junction" cx="310" cy="380" r="4.5"></circle>
<!-- Junction -> End -->
<g class="fc-edge" id="e-merge-end">
<path d="M310,380 L310,424"></path>
<polygon points="310,430 305,420 315,420"></polygon>
</g>
<!-- End -->
<g id="n-end">
<rect class="fc-shape fc-pill" x="240" y="430" width="140" height="42" rx="21"></rect>
<text class="fc-text on-pill" x="310" y="456">End</text>
</g>
</svg>
</div>
<p class="caption" id="caption"></p>
</div>
</div>
<script>
(function () {
const wrapper = document.currentScript.closest('.infographic-jsswitch')
|| document.querySelector('.infographic-jsswitch');
const bodyAttr = document.body.getAttribute('data-darkmode');
let dark;
if (bodyAttr === 'true') {
dark = true;
} else if (bodyAttr === 'false') {
dark = false;
} else {
const match = document.cookie.match(/(?:^|;\s*)darkMode=([^;]+)/);
dark = match ? match[1] === 'true' : false;
}
if (dark) wrapper.setAttribute('data-theme', 'dark');
const input = wrapper.querySelector('#fruitInput');
const caption = wrapper.querySelector('#caption');
const blocks = {
apple: wrapper.querySelector('#n-block1 .fc-block'),
banana: wrapper.querySelector('#n-block2 .fc-block'),
orange: wrapper.querySelector('#n-block3 .fc-block'),
default: wrapper.querySelector('#n-block4 .fc-block')
};
const fanEdges = {
apple: wrapper.querySelector('#e-sw-b1'),
banana: wrapper.querySelector('#e-sw-b2'),
orange: wrapper.querySelector('#e-sw-b3'),
default: wrapper.querySelector('#e-sw-b4')
};
const mergeEdges = {
apple: wrapper.querySelector('#e-b1-merge'),
banana: wrapper.querySelector('#e-b2-merge'),
orange: wrapper.querySelector('#e-b3-merge'),
default: wrapper.querySelector('#e-b4-merge')
};
const eStartSw = wrapper.querySelector('#e-start-sw');
const eMergeEnd = wrapper.querySelector('#e-merge-end');
const junction = wrapper.querySelector('#junction');
const allEdges = [eStartSw, eMergeEnd, ...Object.values(fanEdges), ...Object.values(mergeEdges)];
function reset() {
Object.values(blocks).forEach(b => b.classList.remove('reached'));
junction.classList.remove('is-taken');
allEdges.forEach(e => e.classList.remove('is-check', 'is-taken'));
}
function update() {
reset();
eStartSw.classList.add('is-check');
const raw = input.value.trim().toLowerCase();
const key = blocks.hasOwnProperty(raw) ? raw : 'default';
fanEdges[key].classList.add('is-check');
blocks[key].classList.add('reached');
mergeEdges[key].classList.add('is-taken');
eMergeEnd.classList.add('is-taken');
junction.classList.add('is-taken');
if (key === 'default') {
caption.innerHTML = `<strong>fruit = "${input.value}"</strong> β no case matched, default runs`;
} else {
caption.innerHTML = `<strong>fruit = "${input.value}"</strong> β case '${key}' matched`;
}
}
input.addEventListener('input', update);
wrapper.querySelectorAll('.preset-btn').forEach(btn => {
btn.addEventListener('click', () => { input.value = btn.dataset.v; update(); });
});
update();
})();
</script>.demo {
font-family: system-ui, sans-serif;
padding: 16px;
}
.demo h1 {
color: #2563eb;
margin: 0 0 8px;
}
.demo p {
color: #4b5563;
margin: 0;
}document.querySelector('.demo h1')?.addEventListener('click', () => {
alert('Hello from live preview!');
});π Key Notes:
breakstops the execution after the match β very important!defaultruns if no case matchesswitchworks best when comparing a single value against many fixed values