⌺ Functions Overview
A function is created using the
functionkeyword, followed by a name and parentheses().Function names follow the same rules as variable names (letters, numbers,
_,$).You can add parameters inside the parentheses, separated by commas:
(param1, param2, ...)The code the function runs goes inside curly braces
{}.
<div class="infographic-jsfunc">
<style>
.infographic-jsfunc {
--ink: #1a1a1a;
--ink-soft: #5c5c5c;
--paper: #ffffff;
--paper-alt: #f0ece4;
--accent: #2563eb;
--accent-soft:#e3ebfd;
--line: #ddd6c9;
--ok: #1a7f4e;
--ok-soft: #e3f5ec;
--c-keyword: #7c3aed;
--c-keyword-soft: #ede9fe;
--c-name: #2563eb;
--c-name-soft: #e3ebfd;
--c-params: #b45309;
--c-params-soft: #fdf0dc;
--c-body: #1a7f4e;
--c-body-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-jsfunc[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;
--c-keyword: #a78bfa;
--c-keyword-soft: #2c2140;
--c-name: #5b8def;
--c-name-soft: #202942;
--c-params: #e0a458;
--c-params-soft: #3a2c14;
--c-body: #5cd6a0;
--c-body-soft: #16342a;
}
.infographic-jsfunc * { box-sizing: border-box; }
.infographic-jsfunc .ig-inner { padding: 1.75rem clamp(1.25rem, 5vw, 3rem) 2rem; }
.infographic-jsfunc .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 2rem;
}
/* Anatomy line */
.infographic-jsfunc .code-line {
font-family: var(--mono-font); font-size: clamp(1rem, 3.6vw, 1.25rem);
text-align: center; background: var(--paper-alt); border: 1px solid var(--line);
padding: 1.4rem 1rem; line-height: 1.7;
}
.infographic-jsfunc .part {
cursor: pointer; padding: 0.15rem 0.3rem; border-radius: 4px; transition: background 0.15s, color 0.15s;
}
.infographic-jsfunc .part[data-part="keyword"].is-active { background: var(--c-keyword-soft); color: var(--c-keyword); }
.infographic-jsfunc .part[data-part="name"].is-active { background: var(--c-name-soft); color: var(--c-name); }
.infographic-jsfunc .part[data-part="params"].is-active { background: var(--c-params-soft); color: var(--c-params); }
.infographic-jsfunc .part[data-part="body"].is-active { background: var(--c-body-soft); color: var(--c-body); }
.infographic-jsfunc .legend {
display: grid; grid-template-columns: repeat(4, 1fr); gap: 0.6rem; margin-top: 1rem;
}
@media (max-width: 560px) { .infographic-jsfunc .legend { grid-template-columns: repeat(2, 1fr); } }
.infographic-jsfunc .legend-item {
border: 1px solid var(--line); border-top: 3px solid transparent; background: var(--paper);
padding: 0.6rem 0.7rem; cursor: pointer; text-align: center;
}
.infographic-jsfunc .legend-item .lbl { font-size: 0.78rem; font-weight: 700; }
.infographic-jsfunc .legend-item .desc { font-size: 0.7rem; color: var(--ink-soft); margin-top: 0.15rem; }
.infographic-jsfunc .legend-item[data-part="keyword"].is-active { border-top-color: var(--c-keyword); background: var(--c-keyword-soft); }
.infographic-jsfunc .legend-item[data-part="keyword"].is-active .lbl { color: var(--c-keyword); }
.infographic-jsfunc .legend-item[data-part="name"].is-active { border-top-color: var(--c-name); background: var(--c-name-soft); }
.infographic-jsfunc .legend-item[data-part="name"].is-active .lbl { color: var(--c-name); }
.infographic-jsfunc .legend-item[data-part="params"].is-active { border-top-color: var(--c-params); background: var(--c-params-soft); }
.infographic-jsfunc .legend-item[data-part="params"].is-active .lbl { color: var(--c-params); }
.infographic-jsfunc .legend-item[data-part="body"].is-active { border-top-color: var(--c-body); background: var(--c-body-soft); }
.infographic-jsfunc .legend-item[data-part="body"].is-active .lbl { color: var(--c-body); }
/* Machine */
.infographic-jsfunc .machine-wrap {
margin-top: 2.5rem; display: flex; align-items: center; justify-content: center; gap: 0.75rem; flex-wrap: wrap;
}
.infographic-jsfunc .chip-slot { position: relative; display: flex; flex-direction: column; align-items: center; }
.infographic-jsfunc .slot-tag {
font-size: 0.65rem; font-family: var(--mono-font); color: var(--ink-soft); margin-bottom: 0.35rem;
}
.infographic-jsfunc .arg-input {
width: 6.5rem; height: 2.5rem; font-family: var(--mono-font); font-size: 0.95rem; text-align: center;
border: 1px solid var(--line); background: var(--paper-alt); color: var(--ink); outline: none;
}
.infographic-jsfunc .arg-input:focus { border-color: var(--accent); }
.infographic-jsfunc .flow-arrow {
font-size: 1.3rem; color: var(--line); transition: color 0.3s;
}
.infographic-jsfunc .flow-arrow.is-active { color: var(--accent); }
.infographic-jsfunc .machine-box {
display: flex; flex-direction: column; align-items: center; justify-content: center;
width: 8.5rem; height: 5rem; background: var(--accent-soft); border: 2px solid var(--accent);
border-radius: 10px; transition: box-shadow 0.3s, transform 0.3s;
}
.infographic-jsfunc .machine-box.is-running { box-shadow: 0 6px 16px rgba(37,99,235,0.35); }
.infographic-jsfunc .gear {
font-size: 1.4rem; display: inline-block; transition: transform 0.2s;
}
.infographic-jsfunc .machine-box.is-running .gear { animation: jsfunc-spin 0.6s linear infinite; }
@keyframes jsfunc-spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } }
.infographic-jsfunc .machine-label {
font-family: var(--mono-font); font-size: 0.75rem; font-weight: 700; color: var(--accent); margin-top: 0.2rem;
}
.infographic-jsfunc .result-box {
width: 9rem; min-height: 2.5rem; display: flex; align-items: center; justify-content: center;
font-family: var(--mono-font); font-size: 0.85rem; font-weight: 700; text-align: center;
padding: 0.3rem 0.6rem; background: var(--ok-soft); color: var(--ok); border: 1px solid var(--ok);
border-radius: 6px; opacity: 0; transform: scale(0.85); transition: opacity 0.3s, transform 0.3s;
}
.infographic-jsfunc .result-box.is-visible { opacity: 1; transform: scale(1); box-shadow: 0 3px 10px rgba(0,0,0,0.15); }
.infographic-jsfunc .call-btn {
font: inherit; font-size: 0.85rem; font-weight: 600; border: none; background: var(--accent);
color: var(--paper); padding: 0.55rem 1.3rem; border-radius: 999px; cursor: pointer; margin-top: 1.5rem;
display: block; margin-left: auto; margin-right: auto;
}
.infographic-jsfunc .call-btn:disabled { opacity: 0.6; cursor: default; }
.infographic-jsfunc .call-btn:hover:not(:disabled) { opacity: 0.9; }
.infographic-jsfunc .caption {
text-align: center; font-size: 0.85rem; color: var(--ink-soft); margin-top: 1rem; min-height: 1.2em;
}
.infographic-jsfunc .caption strong { color: var(--ink); font-family: var(--mono-font); }
@media (prefers-reduced-motion: reduce) {
.infographic-jsfunc *, .infographic-jsfunc *::before, .infographic-jsfunc *::after {
animation-duration: 0.001ms !important; transition-duration: 0.001ms !important;
}
}
</style>
<div class="ig-inner">
<h2 class="ig-title">Functions</h2>
<div class="code-line" id="codeLine">
<span class="part" data-part="keyword">function</span>
<span class="part" data-part="name">greet</span>(<span class="part" data-part="params">name</span>) {
<span class="part" data-part="body">return "Hi, " + name + "!";</span> }
</div>
<div class="legend">
<div class="legend-item" data-part="keyword">
<div class="lbl">Keyword</div>
<div class="desc">starts the declaration</div>
</div>
<div class="legend-item" data-part="name">
<div class="lbl">Name</div>
<div class="desc">same rules as variables</div>
</div>
<div class="legend-item" data-part="params">
<div class="lbl">Parameters</div>
<div class="desc">inputs, comma-separated</div>
</div>
<div class="legend-item" data-part="body">
<div class="lbl">Body</div>
<div class="desc">the code that runs</div>
</div>
</div>
<div class="machine-wrap">
<div class="chip-slot">
<span class="slot-tag">name</span>
<input type="text" class="arg-input" id="argInput" value="Alice">
</div>
<span class="flow-arrow" id="arrowIn">→</span>
<div class="machine-box" id="machineBox">
<span class="gear" id="gear">⚙</span>
<span class="machine-label">greet()</span>
</div>
<span class="flow-arrow" id="arrowOut">→</span>
<div class="result-box" id="resultBox"></div>
</div>
<button class="call-btn" id="callBtn">Call greet("Alice")</button>
<p class="caption" id="caption">Give it a name, then call the function.</p>
</div>
</div>
<script>
(function () {
const wrapper = document.currentScript.closest('.infographic-jsfunc')
|| document.querySelector('.infographic-jsfunc');
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');
// Anatomy click-to-highlight (bidirectional between code parts and legend)
function setActivePart(part) {
wrapper.querySelectorAll('[data-part]').forEach(el => {
el.classList.toggle('is-active', el.dataset.part === part);
});
}
wrapper.querySelectorAll('.part, .legend-item').forEach(el => {
el.addEventListener('click', () => setActivePart(el.dataset.part));
});
// The function machine
const argInput = wrapper.querySelector('#argInput');
const callBtn = wrapper.querySelector('#callBtn');
const arrowIn = wrapper.querySelector('#arrowIn');
const arrowOut = wrapper.querySelector('#arrowOut');
const machineBox = wrapper.querySelector('#machineBox');
const resultBox = wrapper.querySelector('#resultBox');
const caption = wrapper.querySelector('#caption');
function updateBtnLabel() {
const val = argInput.value || '';
callBtn.textContent = `Call greet("${val}")`;
}
argInput.addEventListener('input', updateBtnLabel);
updateBtnLabel();
function runMachine() {
const name = argInput.value || '';
callBtn.disabled = true;
resultBox.classList.remove('is-visible');
arrowOut.classList.remove('is-active');
caption.textContent = 'Passing the argument in…';
arrowIn.classList.add('is-active');
setTimeout(() => {
machineBox.classList.add('is-running');
caption.textContent = 'Running the function body…';
}, 250);
setTimeout(() => {
machineBox.classList.remove('is-running');
arrowIn.classList.remove('is-active');
arrowOut.classList.add('is-active');
resultBox.textContent = `"Hi, ${name}!"`;
resultBox.classList.add('is-visible');
caption.innerHTML = `<strong>greet("${name}")</strong> returned this string`;
callBtn.disabled = false;
}, 1050);
}
callBtn.addEventListener('click', runMachine);
})();
</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!');
});✅ Example
function name(param1, param2) {
// code to run
}Parameters → placeholders in the function definition.
Arguments → actual values passed when the function is called.
Inside a function, parameters act like local variables.
➡️ Function Declaration
A Function Declaration is the traditional way to define a function. It starts with the function keyword, followed by the function name and any parameters the function needs.
Example: Below is an example that illustrates the use of Function Declaration.
// Function declaration
function add(a, b) {
console.log(a + b);
}
// Calling a function
add(2, 3);5▶️ Function Expression
Function Expression is another way to define a function. Here, the function is defined inside a variable, and the function's value (its returned value) is stored in that variable.
Example: Below is an example that illustrates the use of Function Expression.
// Function Expression
const add = function (a, b) {
console.log(a + b);
}
// Calling function
add(2, 3);5🧠 Explanation:
function add(a, b)defines a function namedaddthat takes two parameters.console.log(a + b);prints the sum of the two arguments.add(2, 3);calls the function and passes2and3, so it prints5.
🗝️ Key Differences: Function Declaration vs. Function Expression
Function Declaration: The function is defined and can be called anywhere in the code (even before it’s defined, due to something called "hoisting").
Function Expression: The function is defined as part of an expression (usually assigned to a variable), and it can only be called after the line where it’s defined.
❇️ Function Return
The
returnstatement ends the function immediately.Whatever value you put after
returnis sent back to the place where the function was called.This returned value can be stored in a variable, printed, or used in another expression.
✅ Example
function myFunction(a, b) {
// returns the product of a and b
return a * b;
}
let x = myFunction(4, 3);
console.log(x); // prints 12
const y = 10;
console.log(y); // prints 1012
10📝 Explanation
myFunction(4, 3)is called with arguments4and3.Inside the function, it calculates
4 * 3and hitsreturn a * b.The
returnkeyword sends 12 back to the caller.That returned value is stored in variable
x, soconsole.log(x)shows 12.After that,
yis logged, printing 10.