📦 Understanding Variables
A variable is a label that references a value like a number or string. Before using a variable, you need to declare it.
In JavaScript, variable names follow these rules:
Variable names are case-sensitive. This means that the
messageandMessageare different variables.Variable names can only contain (a-z, A-Z), (0-9), (
_), or ($)and cannot contain spaces. Also, variable names must begin with a letter, an underscore (_) or a dollar sign ($).Variable names cannot use the reserved words.
<div class="infographic-varconcept">
<style>
.infographic-varconcept {
--ink: #1a1a1a;
--ink-soft: #5c5c5c;
--paper: #ffffff;
--paper-alt: #f0ece4;
--accent: #2563eb;
--accent-soft:#e3ebfd;
--line: #ddd6c9;
--display-font: 'Fraunces', Georgia, serif;
--body-font: 'Inter', -apple-system, sans-serif;
--mono-font: 'IBM Plex Mono', 'Courier New', monospace;
max-width: 780px;
margin: 0 auto;
box-sizing: border-box;
font-family: var(--body-font);
color: var(--ink);
background: var(--paper);
}
.infographic-varconcept[data-theme="dark"] {
--ink: #e8e6e1;
--ink-soft: #9a968c;
--paper: #121212;
--paper-alt: #1e1e26;
--accent: #5b8def;
--accent-soft:#202942;
--line: #2c313d;
}
.infographic-varconcept * { box-sizing: border-box; }
.infographic-varconcept .ig-inner { padding: 0 clamp(1.25rem, 5vw, 3rem) 2rem; }
.infographic-varconcept .ig-header { text-align: center; }
.infographic-varconcept .ig-eyebrow { font-size: 0.72rem; letter-spacing: 0.12em; text-transform: uppercase; color: var(--accent); font-weight: 600; }
.infographic-varconcept .ig-title { font-family: var(--display-font); font-size: clamp(1.6rem, 5vw, 2.2rem); font-weight: 600; }
.infographic-varconcept .ig-sub { color: var(--ink-soft); font-size: 0.9rem; max-width: 40ch; margin: 0 auto; }
.infographic-varconcept .stage {
display: flex; justify-content: center;
}
.infographic-varconcept .stage svg { width: 100%; max-width: 560px; height: auto; overflow: visible; }
.infographic-varconcept .tag-label {
font-family: var(--mono-font); font-size: 1.1rem; font-weight: 600; fill: var(--ink);
}
.infographic-varconcept .tag-shape { fill: var(--paper-alt); stroke: var(--ink); stroke-width: 1.5; }
.infographic-varconcept .tag-hole { fill: var(--paper); stroke: var(--ink); stroke-width: 1.5; }
.infographic-varconcept .string-path { stroke: var(--ink-soft); stroke-width: 1.5; fill: none; stroke-dasharray: 3 3; }
.infographic-varconcept .value-box {
fill: var(--accent-soft); stroke: var(--accent); stroke-width: 2;
transition: all 0.2s ease;
}
.infographic-varconcept .value-box.is-swapping { opacity: 0; }
.infographic-varconcept .value-text {
font-family: var(--mono-font); font-size: 1.3rem; font-weight: 600; fill: var(--accent);
transition: opacity 0.15s ease;
}
.infographic-varconcept .value-text.is-swapping { opacity: 0; }
.infographic-varconcept .value-type {
font-family: var(--body-font); font-size: 0.7rem; fill: var(--ink-soft);
}
.infographic-varconcept .controls { display: flex; justify-content: center; margin-top: 1rem; }
.infographic-varconcept .swap-btn {
font: inherit; font-size: 0.85rem; font-weight: 600; color: var(--paper);
background: var(--accent); border: none; padding: 0.55rem 1.2rem; cursor: pointer;
}
.infographic-varconcept .swap-btn:hover { opacity: 0.9; }
.infographic-varconcept .caption {
text-align: center; font-size: 0.85rem; color: var(--ink-soft); margin-top: 1rem;
min-height: 1.2em;
}
.infographic-varconcept .caption strong { color: var(--ink); }
@media (prefers-reduced-motion: reduce) {
.infographic-varconcept *, .infographic-varconcept *::before, .infographic-varconcept *::after {
animation-duration: 0.001ms !important; transition-duration: 0.001ms !important;
}
}
</style>
<div class="ig-inner">
<div class="stage">
<svg viewBox="0 0 560 220" xmlns="http://www.w3.org/2000/svg">
<!-- string connecting tag to value -->
<path class="string-path" d="M 130 110 Q 220 60, 300 110" />
<!-- luggage tag = the variable name -->
<g transform="translate(40,80)">
<path class="tag-shape" d="M0,0 L70,0 L90,30 L70,60 L0,60 Z" />
<text class="tag-label" x="40" y="36" text-anchor="middle">msg</text>
</g>
<!-- value box -->
<g id="valueGroup" transform="translate(300,55)">
<rect id="valueBox" class="value-box" x="0" y="0" width="160" height="110" rx="4"></rect>
<text id="valueText" class="value-text" x="80" y="62" text-anchor="middle">"Hi"</text>
<text id="valueType" class="value-type" x="80" y="98" text-anchor="middle">string</text>
</g>
</svg>
</div>
<div class="controls">
<button class="swap-btn" id="swapBtn">Give it a new value →</button>
</div>
<p class="caption" id="caption">The label <strong>msg</strong> now points to a string.</p>
</div>
</div>
<script>
(function () {
const wrapper = document.currentScript.closest('.infographic-varconcept')
|| document.querySelector('.infographic-varconcept');
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 values = [
{ text: '"Hi"', type: 'string', caption: 'points to a string' },
{ text: '42', type: 'number', caption: 'points to a number' },
{ text: 'true', type: 'boolean', caption: 'points to a boolean' },
{ text: '[ 1, 2 ]', type: 'array', caption: 'points to an array' },
{ text: '{ a: 1 }', type: 'object', caption: 'points to an object' }
];
let i = 0;
const box = wrapper.querySelector('#valueBox');
const text = wrapper.querySelector('#valueText');
const typeText = wrapper.querySelector('#valueType');
const caption = wrapper.querySelector('#caption');
const btn = wrapper.querySelector('#swapBtn');
btn.addEventListener('click', () => {
box.classList.add('is-swapping');
text.classList.add('is-swapping');
setTimeout(() => {
i = (i + 1) % values.length;
const v = values[i];
text.textContent = v.text;
typeText.textContent = v.type;
caption.innerHTML = `The label <strong>msg</strong> now ${v.caption}.`;
box.classList.remove('is-swapping');
text.classList.remove('is-swapping');
}, 180);
});
})();
</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!');
});🛠️ Declaring a variable
To declare a variable, you use the var , let, constkeyword followed by the variable name .
✅ You can use thevar keyword to declare a variable like this:
var variableName;A variable name can be any valid identifier. For example:
var message;By default, a variable has a special value undefined if you don’t assign it a value.
✅ You can use the let keyword to declare a variable like this:
let message;It’s a good practice to use the let keyword to declare a variable.
✅A constant holds a value that doesn’t change. To declare a constant, you use the const keyword. When defining a constant, you need to initialize it with a value immediately. For example:
const workday = 5;Once you define a constant, you cannot change its value.
🏷️ Initializing a variable
After declaring a variable, you can initialize it with a value. To initialize a variable, you specify the variable name, followed by an equals sign (=) and a value:
variableName = initialValue;For example,
let message;
message = "Hello";JavaScript allows you to declare two or more variables using a single statement. To separate two variable declarations, you use a comma (
,) like this:
let message = "Hello",
counter = 100;🧩 Changing a variable
After declaring or initializing a variable, you can change its value by setting a different value. For example:
let message = "Hello";
message = 'Bye';♦️Undefined vs. undeclared variables
An undefined variable is a variable that has been declared but has not been initialized with a value. For example:
let message;
console.log(message);// undefinedIn this example, the message variable is declared but not initialized. Therefore, the message variable is undefined.
In contrast, an undeclared variable is a variable that has not been declared. For example:
console.log(counter);console.log(counter);
^
ReferenceError: counter is not definedIn this example, the counter variable has not been declared. Hence, accessing it causes a ReferenceError.