π‘ Introduction to JavaScript
JavaScript (JS) is a lightweight, high-level, programming language used to make web pages interactive, dynamic, and functional.
It runs directly in the browser, making it a core technology of the web along with HTML and CSS.
π§© Why do we use JavaScript?
JavaScript brings life to a webpage. Without it:
HTML β Only structure
CSS β Only design
JavaScript β Adds behavior
<div class="infographic-htmlimportance">
<style>
.infographic-htmlimportance {
--ink: #1a1a1a;
--ink-soft: #5c5c5c;
--paper-alt: #f0ece4;
--accent: #2563eb;
--accent-deep: #1942a8;
--accent-soft: #e3ebfd;
--line: #ddd6c9;
--display-font: 'Fraunces', Georgia, serif;
--body-font: 'Inter', system-ui, sans-serif;
--mono-font: 'IBM Plex Mono', monospace;
background: transparent;
color: var(--ink);
font-family: var(--body-font);
}
.infographic-htmlimportance * { box-sizing: border-box; }
.hi-head { margin-bottom: 1.5rem; }
.hi-eyebrow {
font-family: var(--mono-font); font-size: 0.72rem; letter-spacing: 0.1em;
text-transform: uppercase; color: var(--accent); margin: 0 0 0.45rem;
display: flex; align-items: center; gap: 0.4rem;
}
.hi-eyebrow::before { content: ""; width: 14px; height: 2px; background: var(--accent); display: inline-block; }
.hi-title {
font-family: var(--display-font); font-size: clamp(1.5rem, 4vw, 2.3rem);
margin: 0 0 0.55rem; line-height: 1.12; letter-spacing: -0.01em;
}
.hi-sub { color: var(--ink-soft); font-size: 0.95rem; max-width: 560px; line-height: 1.55; margin: 0; }
.hi-points { list-style: none; margin: 1.1rem 0 1.75rem; padding: 0; display: flex; flex-direction: column; gap: 0.5rem; }
.hi-points li {
font-size: 0.86rem; color: var(--ink-soft); display: flex; gap: 0.55rem; align-items: flex-start;
line-height: 1.5;
}
.hi-points li::before { content: "β"; color: var(--accent); flex-shrink: 0; font-weight: 600; }
.hi-points li b { color: var(--ink); font-weight: 600; }
/* ===== Tabs / stepper ===== */
.hi-tabs { display: flex; gap: 0.4rem; margin-bottom: 1.1rem; flex-wrap: wrap; }
.hi-tab {
font: inherit; font-size: 0.78rem; font-weight: 600; font-family: var(--mono-font);
background: var(--paper-alt); border: 1.5px solid var(--line); border-radius: 100px;
padding: 0.45rem 1rem; cursor: pointer; color: var(--ink-soft);
transition: all 0.2s; display: flex; align-items: center; gap: 0.4rem;
}
.hi-tab .hi-tab-num {
width: 16px; height: 16px; border-radius: 50%; background: var(--line); color: #fff;
font-size: 0.65rem; display: flex; align-items: center; justify-content: center;
transition: background 0.2s;
}
.hi-tab:hover { border-color: var(--accent); color: var(--ink); }
.hi-tab.is-active { background: var(--accent); border-color: var(--accent); color: #fff; }
.hi-tab.is-active .hi-tab-num { background: rgba(255,255,255,0.3); }
/* ===== Stage wrap ===== */
.hi-stage-wrap { width: 100%; }
.hi-stage {
position: relative; border-radius: 8px; overflow: hidden;
border: 2px solid var(--ink); transition: border-color 0.4s ease, box-shadow 0.4s ease;
box-shadow: 0 10px 24px -10px rgba(26,26,26,0.16);
}
/* ---- Stage 1: HTML only β bland, unstyled, browser-default look ---- */
.hi-page { background: #fff; font-family: Times, "Times New Roman", serif; color: #000; transition: all 0.4s ease; }
.hi-page * { transition: all 0.4s ease; }
.hi-h1 { font-size: 1.4rem; margin: 0.7rem 0.8rem 0.3rem; font-weight: bold; }
.hi-nav { margin: 0 0.8rem 0.6rem; display: flex; gap: 0.9rem; }
.hi-nav a { color: #0000EE; text-decoration: underline; font-size: 0.85rem; }
.hi-p { margin: 0 0.8rem 0.5rem; font-size: 0.88rem; line-height: 1.4; color: #000; }
.hi-img-box { margin: 0.6rem 0.8rem; width: 64px; height: 44px; border: 1px solid #000; background: #fff; }
.hi-btn-el {
display: inline-block; margin: 0.4rem 0.8rem 0.8rem; font-family: inherit; font-size: 0.85rem;
background: #efefef; border: 2px outset #ccc; border-radius: 0; padding: 0.25rem 0.7rem;
color: #000; cursor: pointer;
}
/* ---- Stage 2: + CSS β colorful, spaced, brand-styled ---- */
.hi-stage[data-stage="2"] .hi-page,
.hi-stage[data-stage="3"] .hi-page {
font-family: var(--body-font); background: #fff; padding: 1.4rem; max-width: 560px; margin: 0 auto;
}
.hi-stage[data-stage="2"] .hi-h1,
.hi-stage[data-stage="3"] .hi-h1 {
font-family: var(--display-font); font-weight: 700; color: var(--accent-deep);
margin: 0 0 0.5rem; font-size: 1.5rem;
}
.hi-stage[data-stage="2"] .hi-nav,
.hi-stage[data-stage="3"] .hi-nav {
margin: 0 0 1rem; gap: 0.5rem;
}
.hi-stage[data-stage="2"] .hi-nav a,
.hi-stage[data-stage="3"] .hi-nav a {
color: var(--accent); text-decoration: none; font-weight: 600; font-size: 0.78rem;
background: var(--accent-soft); padding: 0.25rem 0.65rem; border-radius: 100px;
}
.hi-stage[data-stage="2"] .hi-p,
.hi-stage[data-stage="3"] .hi-p {
margin: 0 0 0.8rem; font-size: 0.86rem; line-height: 1.55; color: var(--ink-soft);
}
.hi-stage[data-stage="2"] .hi-img-box,
.hi-stage[data-stage="3"] .hi-img-box {
margin: 0 0 0.9rem; width: 100%; height: 90px; border: none; border-radius: 6px;
background: linear-gradient(135deg, var(--accent-soft), var(--accent));
}
.hi-stage[data-stage="2"] .hi-btn-el,
.hi-stage[data-stage="3"] .hi-btn-el {
display: inline-block; margin: 0; font-weight: 600; border-radius: 6px; border: none;
padding: 0.55rem 1.1rem; background: var(--accent); color: #fff; box-shadow: 0 4px 10px -2px rgba(37,99,235,0.4);
}
/* ---- Stage 3: + JS β same styled look, but the button now does something ---- */
.hi-stage[data-stage="3"] .hi-btn-el {
background: #f5c518; color: #1a1a1a; box-shadow: 0 4px 10px -2px rgba(245,197,24,0.5);
}
.hi-stage[data-stage="3"] .hi-btn-el { cursor: pointer; }
.hi-stage[data-stage="3"] .hi-btn-el:active { transform: scale(0.96); }
.hi-stage:not([data-stage="3"]) .hi-btn-el { cursor: default; }
/* dark mode β only meaningful on stage 3, toggled by JS */
.hi-stage[data-stage="3"] .hi-page[data-dark="true"] {
background: #15171c;
}
.hi-stage[data-stage="3"] .hi-page[data-dark="true"] .hi-h1 { color: #7fa8ff; }
.hi-stage[data-stage="3"] .hi-page[data-dark="true"] .hi-nav a {
color: #cfe0ff; background: #232735;
}
.hi-stage[data-stage="3"] .hi-page[data-dark="true"] .hi-p { color: #a7adba; }
.hi-stage[data-stage="3"] .hi-page[data-dark="true"] .hi-img-box {
background: linear-gradient(135deg, #232735, var(--accent-deep));
}
.hi-stage-label {
font-family: var(--mono-font); font-size: 0.7rem; letter-spacing: 0.05em; text-transform: uppercase;
color: var(--ink-soft); text-align: center; margin: 0.8rem 0 0;
}
.hi-stage-label b { color: var(--accent); }
@media (max-width: 500px) {
.hi-tabs { gap: 0.35rem; }
.hi-tab { padding: 0.4rem 0.75rem; font-size: 0.72rem; }
}
.hi-takeaway {
margin-top: 1.6rem; padding: 0.95rem 1.2rem; background: var(--accent-soft);
border-left: 3px solid var(--accent); border-radius: 4px;
font-size: 0.9rem; color: var(--ink); line-height: 1.5;
}
.hi-takeaway b { color: var(--accent-deep); }
@media (prefers-reduced-motion: reduce) {
.infographic-htmlimportance *, .infographic-htmlimportance *::before, .infographic-htmlimportance *::after {
transition-duration: 0.001ms !important; animation-duration: 0.001ms !important;
}
}
</style>
<div class="hi-tabs" id="hiTabs">
<button class="hi-tab is-active" data-stage="1"><span class="hi-tab-num">1</span> HTML only</button>
<button class="hi-tab" data-stage="2"><span class="hi-tab-num">2</span> + CSS</button>
<button class="hi-tab" data-stage="3"><span class="hi-tab-num">3</span> + CSS + JS</button>
</div>
<div class="hi-stage-wrap">
<div class="hi-stage" id="hiStage" data-stage="1">
<div class="hi-page">
<div class="hi-h1">My Page</div>
<div class="hi-nav"><a>Home</a><a>About</a><a>Contact</a></div>
<div class="hi-img-box"></div>
<p class="hi-p">This paragraph is just content β HTML says "this is a paragraph," nothing about how it should look.</p>
<button class="hi-btn-el" id="hiTryBtn">π Toggle dark mode</button>
</div>
</div>
<p class="hi-stage-label" id="hiStageLabel">Structure only β every browser renders this the same dull way <b>by default</b></p>
</div>
</div>
<script>
(function() {
var roots = document.querySelectorAll('.infographic-htmlimportance');
var root = roots[roots.length - 1];
if (!root) return;
var tabs = root.querySelectorAll('.hi-tab');
var stage = root.querySelector('#hiStage');
var stageLabel = root.querySelector('#hiStageLabel');
var tryBtn = root.querySelector('#hiTryBtn');
var page = root.querySelector('.hi-page');
var labels = {
'1': 'Structure only β every browser renders this the same dull way <b>by default</b>',
'2': 'Same HTML, now <b>styled</b> β colors, spacing, type all came from CSS',
'3': 'Same HTML + CSS, now <b>alive</b> β click the button, JS is doing that'
};
function setStage(num) {
stage.dataset.stage = num;
stageLabel.innerHTML = labels[num];
tabs.forEach(function(t) { t.classList.toggle('is-active', t.dataset.stage === num); });
if (num !== '3') {
page.dataset.dark = 'false';
tryBtn.textContent = 'π Toggle dark mode';
}
}
tabs.forEach(function(tab) {
tab.addEventListener('click', function() { setStage(tab.dataset.stage); });
});
tryBtn.addEventListener('click', function() {
if (stage.dataset.stage !== '3') return; // button only "works" once JS stage is active
var isDark = page.dataset.dark === 'true';
page.dataset.dark = isDark ? 'false' : 'true';
tryBtn.textContent = isDark ? 'π Toggle dark mode' : 'βοΈ Toggle light mode';
});
})();
</script>
</div>.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!');
});Things JavaScript can do:
β Change HTML content dynamically
β Change CSS styles on the fly
β Show/hide elements
β Validate forms
β Fetch data from APIs
β Move/animate elements
β Build full web applications
π Where does JavaScript run?
1. In the Browser
Every modern browser has a JavaScript Engine:
Chrome β V8
Firefox β SpiderMonkey
Safari β JavaScriptCore
Edge β Chakra (old) / V8 (new)
2. On the Server
With Node.js, JavaScript can run:
server-side
handle APIs
connect databases
build backend services
π§ Key Features of JavaScript
πΉ High-level language
You donβt manage memory manually β JS handles it.
πΉ Interpreted language
Code runs line by line in the browser (no need to compile).
πΉ Dynamic typing
Variables can store any type:
let x = 10; // number
x = "Hello"; // now a stringπΉ Prototype-based OOP
Uses prototypes instead of classes (though modern JS has class syntax).
πΉ Event-driven
JavaScript reacts to user actions:
clicks
scrolls
keyboard typing
π₯ Example JavaScript Code
document.getElementById("btn").addEventListener("click", () => {
alert("Button clicked!");
});This code runs when a user clicks a button.
π What can you build with JavaScript?
β Websites
β Mobile apps (React Native)
β Desktop apps (Electron)
β Server APIs (Node.js + Express)
β Games (Phaser.js, Three.js)
β Machine Learning models (TensorFlow.js)
π€ How to setup Javascript locally
Option 1: Html + CSS + Javascript (Eventually you will use this also later)
https://www.youtube.com/watch?v=xkMfMJn5Smg
Option 2: Only JavaScript (Preferred for this Course)
https://www.youtube.com/watch?v=0CkeMXkEOrc
π In short
JavaScript is the brain of the web, allowing developers to build interactive, responsive, real-time, and modern applications.
It transforms static pages into dynamic experiences users actually enjoy.