Day 1 · Welcome & Variables
Hands-on
Task 1 Hello World
- Print
"Hello, World!"withconsole.log. - Declare a
constfor your name and log it. - Log one line that uses
+to build:name + " is learning JS".
console.log("Hello, World!");
const name = "Kalyan"; // replace with your name
console.log(name);
console.log(name + " is learning JS");
Check the browser console (F12 → Console) when you run day-1/js/hands-on.js via this page.
Task 2
let vs const
- Create
let age = 25. Log it. - Reassign
age = 26Log it again. Confirm the new value. - create
const PI = 3.14Try to reassignPI = 3.15. Note the error. - Create
let cityName(no value yet). Log it. Then assign"Jaipur". Log again.
let age = 25;
console.log(age); // 25
age = 26;
console.log(age); // 26 (updated)
// const cannot be reassigned
const PI = 3.14;
console.log(PI);
// declare without value
let cityName;
console.log(cityName); // undefined
cityName = "Jaipur";
console.log(cityName); // Jaipur
Reference behaviour is implemented in ../js/hands-on.js (loaded below).
Task 3 Block Scope
- Create a block
{ let x = 10; console.log(x); }. Run it - Now try to log x AFTER the block. Note the error.
- Inside an
if (true) { ... }block, declarelet inside = "hi"and log it. - Try logging inside outside the if. Note the error.
{
let x = 10;
console.log(x); // 10
}
if (true) {
let inside = "hi";
console.log(inside); // hi
}
Reference behaviour is implemented in ../js/hands-on.js (loaded below).
Bonus
var Pitfall
- Inside a
for (var i = 0; i < 3; i++) { }loop, log i AFTER the loop ends. - Note that i is still accessible — it leaked out!
- Now do the same with
let i. Try logging i after — note the error - In a comment, write WHY this matters.
for (var i = 0; i < 3; i++) {}
console.log(i);