Day 1: What is JavaScript? Your First Program
✅ Today you will learn
- What JavaScript does on a web page (and beyond)
- Where JS runs: browser, Node.js, mobile apps
- Adding JS with <script src defer>
- console.log and the DevTools Console
- Your first interactive button
🏢 In the real world
The Sharma Sweets website now looks beautiful (CSS course) but it is still a brochure: the “Add to cart” button does nothing, the order form cannot calculate a total and the theme cannot be switched. HTML is the structure, CSS the look – JavaScript is the behaviour. Over 30 days you will turn the site into a working online shop.
HTML, CSS and JavaScript
<button id="add">Add Kaju Katli to cart</button>
<p id="msg">Cart is empty.</p>
<script>
let count = 0;
document.querySelector('#add').addEventListener('click', function () {
count = count + 1;
document.querySelector('#msg').textContent = 'Cart: ' + count + ' box(es) of Kaju Katli';
});
</script>| Where JS runs | Used for | Example |
|---|---|---|
| Browser | Interactive websites | Carts, forms, maps, chat widgets |
| Node.js (server) | APIs, back-ends, scripts | Payment webhooks, Discord bots |
| Mobile / desktop | Apps with React Native, Electron | VS Code itself is written in JS/TypeScript |
| Frameworks | React, Next.js, Vue, Angular | Most Indian startup front-ends |
Your first program: console.log
console.log('Namaste, JavaScript!');
console.log('Kaju katli price per kg:', 900);
console.log(2 + 3);
console.log('2' + '3'); // text joins, it does not add!Open any website, press F12 (or Ctrl+Shift+J) and click the Console tab. Type console.log("Hi") and press Enter. Every program on this page also shows its real output in the OUTPUT box under the code.
Adding JavaScript to a page
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Sharma Sweets</title> <link rel="stylesheet" href="css/style.css"> <script src="js/main.js" defer></script> <!-- ⭐ external file + defer --> </head> <body> <button id="add">Add to cart</button> </body> </html>
| Method | When |
|---|---|
| <script src="js/main.js" defer> in head ⭐ | Every real project – downloads in parallel, runs after the HTML is ready |
| <script> … </script> at the end of body | Small demos (our examples) |
| onclick="…" attribute | Avoid – mixes HTML and JS, hard to maintain |
| type="module" | Modern import/export (Day 21) – deferred automatically |
The most common beginner error: the script ran before the button existed, so querySelector returned null. Fix it with defer (or put the script at the end of body).
Statements, comments and case
// A single-line comment – ignored by JavaScript /* A multi-line comment useful for explaining a block */ let shop = 'Sharma Sweets'; // a statement ends with ; let Shop = 'Another shop'; // JS is case-sensitive: shop ≠ Shop console.log(shop); console.log(Shop); console.log(typeof shop);
Real mini-task: a festival greeting
<input id="name" placeholder="Your name" aria-label="Your name">
<button id="greet">Greet me</button>
<h3 id="out"></h3>
<script>
document.querySelector('#greet').addEventListener('click', function () {
const name = document.querySelector('#name').value || 'friend';
document.querySelector('#out').textContent = 'Happy Diwali, ' + name + '! 🪔 Get 10% off today.';
});
</script>- 1querySelector finds an element using a CSS selector
- 2addEventListener waits for something to happen (a click)
- 3The function inside runs every time it happens
- 4.value reads what was typed; .textContent writes text to the page
🏅 Job skills unlocked today
Practice tasks
🏋️ Practice – do it yourself
Show solution
console.log('Name: Aarav Mehta');
console.log('City: Jaipur');
console.log('Total bill:', 1500 + 250);Show solution
Quick quiz
🧠 Quick Quiz – test yourself
Interview corner
💼 Interview Corner
What is the difference between defer and async on a script tag?
Both download without blocking HTML parsing. defer runs scripts in order after the document is parsed (ideal for app code); async runs each script as soon as it downloads, in any order (ideal for independent scripts like analytics).
Is JavaScript the same as Java?
No. They are unrelated languages; the name was a 1995 marketing decision. JavaScript is dynamically typed, runs in browsers and Node.js; Java is statically typed and compiled to JVM bytecode.
Summary
- JavaScript adds behaviour to HTML and CSS.
- Link external files with <script src defer>.
- console.log prints values; F12 → Console.
- querySelector + addEventListener + textContent = first interactivity.
Day 2: variables (let, const), data types and template literals – build a sweet-shop bill.
