Day 2 — Types, strings, and typeof

Homework

Task 1 Primitives — value and typeof
  • In day2.js, declare one variable of each primitive type (number, string, boolean, undefined, null, bigint, symbol).
  • For each variable, console.log its value and its typeof.
const hwNum = 42;
const hwStr = "hello";
const hwBool = true;
let hwUndef;
const hwNull = null;
const hwBig = 10n;
const hwSym = Symbol("hw");

console.log(hwNum, typeof hwNum);
console.log(hwStr, typeof hwStr);
console.log(hwBool, typeof hwBool);
console.log(hwUndef, typeof hwUndef);
console.log(hwNull, typeof hwNull);
console.log(hwBig, typeof hwBig);
console.log(hwSym, typeof hwSym);

Reference: ../js/day2.js (loaded below). You can also run node day2.js from the day-2/js folder.

Task 2 Five sentences — template literals
  • Use three variables: name, age, city.
  • Write five different sentences about them using template literals (each its own console.log).
const name = "Aarav";
const age = 22;
const city = "Jaipur";

console.log(`${name} is ${age} years old and lives in ${city}.`);
console.log(`From ${city}: meet ${name}, age ${age}.`);
console.log(`${name} moved to ${city} at age ${age}.`);
console.log(`Travel tip: ${city} is home for ${name} (${age}).`);
console.log(`Profile — Name: ${name}, City: ${city}, Age next birthday: ${age + 1}.`);

Reference: ../js/day2.js.

Task 3 Floating point and the “paise” trick
  • Compute 0.1 + 0.2 and log the result.
  • Use the multiply-to-paise idea: compute (10 + 20) / 100 and confirm you get exactly 0.3 (e.g. with ===).
console.log(0.1 + 0.2);
console.log((10 + 20) / 100);
console.log(((10 + 20) / 100) === 0.3);

Reference: ../js/day2.js.

Task 4 User card (multi-line template)
  • Build a “user card” string using a multi-line template literal.
  • Include name, age, city, and one fact about you (extra variable is fine).
const fact = "learning JavaScript every day";

const userCard = `
Name: ${name}
Age: ${age}
City: ${city}
Fact: ${fact}
`;
console.log(userCard);

Reference: ../js/day2.js.

Bonus typeof on [], {}, and a function
  • In the console you can type [], {}, and function(){}.
  • In your file, use typeof on each and log — notice what you get.
console.log(typeof []);
console.log(typeof {});
console.log(typeof function () {});

Reference: ../js/day2.js.