Day 2 — Types, strings, and typeof
Hands-on
Task 1
Variables, + concatenation, template literal, and card
- Create
const name = "Aarav",const age = 22,const city = "Jaipur". - Build the string
"Aarav, 22, from Jaipur"using old-style concatenation with+. - Build the same string using a template literal.
- Then build a 3-line “card” with a multi-line template literal:
- Line 1:
Name: <name> - Line 2:
Age: <age> - Line 3:
City: <city>
- Line 1:
const name = "Aarav";
const age = 22;
const city = "Jaipur";
const oldStyle = name + ", " + age + ", from " + city;
console.log(oldStyle);
const templateStyle = `${name}, ${age}, from ${city}`;
console.log(templateStyle);
const card = `
Name: ${name}
Age: ${age}
City: ${city}
`;
console.log(card);
Open the console (F12). This matches ../js/hands-on.js (loaded below).
Task 3
null vs undefined
- Declare
let user;(no value). Log it. Logtypeof user. - Set
user = null. Log it. Logtypeof user. - Compare
null == undefinedandnull === undefined. Note the difference. - In a comment, explain when you should use
nullvs leave itundefined.
let user;
console.log(user); // undefined
console.log(typeof user); // "undefined"
user = null;
console.log(user); // null
console.log(typeof user); // "object"
console.log(null == undefined); // true
console.log(null === undefined); // false
// Use null when you intentionally mean "no value" (cleared field, API placeholder).
// Leave undefined for "not assigned yet" — optional props, declare-before-assign.
Same behaviour in ../js/hands-on.js.
Bonus Number quirks
- Compute
0.1 + 0.2. Log the result. - Compare it to
0.3with===. Note the surprise. - Try
Number.isInteger(5),Number.isInteger(5.0),Number.isInteger(5.5). - Try
1 / 0and"abc" * 2. Note the special values.
console.log(0.1 + 0.2);
console.log((0.1 + 0.2) === 0.3);
console.log(Number.isInteger(5));
console.log(Number.isInteger(5.0));
console.log(Number.isInteger(5.5));
console.log(1 / 0);
console.log("abc" * 2);
Same behaviour in ../js/hands-on.js.