Class 15 — many facts, one tidy container
One dish isn't one fact. It's a name, a price, a category, a yes/no — all at once. Today you keep them together.
In Class 14 a variable held one value — let price = 90;. But describe a real menu dish and you instantly need several facts that belong together: its name, its price, its category, whether it's a millet dish. Keeping them in four loose variables (dishName, dishPrice, dishCategory…) gets messy fast and nothing says they're related. JavaScript's answer is the object: one container that holds many labelled values as a single unit. Today you'll build one, read from it two ways, give it a method, and nest one object inside another.
Walk out of this room able to…
Every demo runs live, right on this slide. To practise on your own machine, make a fresh throwaway folder fswd-practice\Class-15\, open it in VS Code, and create a scratch script.js there — build tiny files from scratch, run them, throw them away. Nothing here is committed: fswd-practice\ is scratch paper, never a git repo. The real poshtik-campus\ project — and its commits — live only in the lab classes. Never here.
To see any output on your own machine: open your .html file in the browser, then press F12 (or right-click → Inspect) and click the Console tab — that's where every console.log appears.
"Which .html file?" — the two-file pair, restated in 8 lines
Every panel today is captioned script.js, and a .js file can't run by itself — a page has to load it. You built this exact page in Class 13 and again in Class 14, so we won't labour it; here it is once, complete, so nothing today is a mystery. Objects live entirely in the Console — they have no appearance on a page — so this host page stays deliberately bare. It exists only to start script.js.
<!DOCTYPE html><html lang="en"><head> <title>Class 15 — objects</title></head><body> <script src="script.js"></script> <!-- the only line that matters --></body></html>9 lines, no CSS at all today — an object has no colour, no size, no position. Nothing to style. Save this beside your script.js in the SAME folder.(a blank white page — and that is the correct result)
Why no CSS today — and why that's a real answer, not a shortcut. A stylesheet decides how elements look. An object is pure data sitting in memory; it never appears on the page, so there is genuinely nothing for CSS to style. This is also the honest reason today's page is 9 lines instead of Unit 1's rich markup: we build only the HTML the concept actually needs. Tomorrow, in Class 16, objects meet the page — and CSS comes straight back.
"But how do I see an object without HTML and CSS?" — the honest answer, both ways
Fair question, and it deserves a real demonstration rather than a promise. A pure-JS lesson has two legitimate output surfaces, and you should know both: (1) the Console — which is what today uses, and it is a genuine output screen, not a placeholder; and (2) a page, once you decide to show the data to a visitor. Below is the same dish object rendered the second way, so the difference is concrete. Note the order — plain HTML first, then CSS, then the JavaScript that fills it in.
<body> <h2 id="dish-name">—</h2> <!-- empty holders --> <p id="dish-price">—</p> <script src="script.js"></script></body>/* Plain HTML above: two empty placeholders. Now style.css ↓ */ #dish-name { color: seagreen; } #dish-price { font-weight: bold; }/* Styled, but still showing "—". Now script.js pours the OBJECT in ↓ */const dish = { name: "Millet Khichdi", price: 90 };document.getElementById("dish-name").textContent = dish.name;document.getElementById("dish-price").textContent = "₹" + dish.price;THE WHOLE PROGRESSION IN ONE PANEL: HTML makes the holders, CSS gives them a look, JS fills them from the object. Class 16 teaches lines 9-10 properly.—Millet Khichdi
—₹90
↑ PLAIN HTML — EMPTY HOLDERS, DEFAULT BLACK, NO CSS AND NO JS YETThree distinct phases in one preview: bare dashes → the dashes get colour and weight → the object's real values replace them. That is what "visualising an object" means once you leave the Console.
The rule this class follows, stated plainly: HTML/CSS is built only to the level the concept needs — no more. For objects, that level is nearly zero, so the Console is the honest output screen. For the DOM (Class 16), the level is a real page, so a real page gets built. Either way the order never changes: plain HTML → CSS → JavaScript.
The idea, in one plain sentence
One real thing has many facts that belong together. An object keeps them in one container.
Think of a single dish on the Poshtik Campus menu — say Millet Khichdi. It isn't one fact. It has a name, a price, a category, and a yes/no flag for "is it a millet dish?". In Class 14 each of those would be its own loose variable. That works for one dish… but the four boxes just float around with nothing tying them together. JavaScript's answer is the object: one box that holds many labelled values as a single unit.
The messy way vs the tidy way — same four facts, side by side
First the loose-variable approach you already know — and because JavaScript can never run on its own, we build its host page from scratch first too: plain HTML → CSS → JS. Step through it: four separate boxes, and the only thing linking them is that you remembered to start each name with dish. In the next beat the same four facts get poured into one object.
<!-- FILE 1 of 3 · loose.html — the page that will CARRY script.js --><!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Four loose variables</title></head><body> <h1>The messy way</h1> <p class="tip">Four unlinked boxes — check the Console (F12).</p></body></html>/* HTML complete & PLAIN above. FILE 2 of 3 · the CSS — one rule per press ↓ */ <style> body { font-family: Georgia, serif; padding: 18px; } h1 { color: #B91C1C; font-size: 20px; } .tip { color: #64748B; font-size: 13px; } </style><!-- CSS done. Hire the passenger — last line before </body> --> <script src="script.js"></script>// ————— FILE 3 of 3 · script.js — four separate, unlinked boxes —————// one dish = four separate, unlinked boxeslet dishName = "Millet Khichdi";let dishPrice = 90;let dishCategory = "bowl";let dishIsMillet = true;console.log(dishName);console.log(dishPrice);nothing in the code says these four boxes describe the SAME dish. Add a second dish and you'd need dish2Name, dish2Price… it gets messy fastThe messy way
Four unlinked boxes — check the Console (F12).
PLAIN HTML — NO CSS AND NO SCRIPT YETNow the same dish as one object — page and all, built from scratch
Same fixed order as always: plain HTML first (the page that will carry the script), then CSS one rule per press, then the JavaScript. Watch three things on the right: the white page appears plain, then each CSS rule lands, then the dark Console shows the object growing one property at a time and printing as a single tidy unit.
<!-- FILE 1 of 3 · dish.html — the page that will CARRY script.js --><!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>One dish, one object</title></head><body> <h1>Poshtik Campus</h1> <p class="tip">The dish object prints in the Console (F12).</p></body></html>/* HTML complete & PLAIN above. FILE 2 of 3 · the CSS — one rule per press ↓ */ <style> /* typed back up in the <head> */ body { font-family: Georgia, serif; padding: 18px; } h1 { color: seagreen; font-size: 20px; } .tip { color: #64748B; font-size: 13px; } </style><!-- CSS done. Hire the passenger — last line before </body> --> <script src="script.js"></script>// ————— FILE 3 of 3 · script.js — one object, four labelled facts —————// one dish = ONE object holding four labelled factsconst dish = { name: "Millet Khichdi", price: 90, category: "bowl", isMillet: true};console.log(dish);ONE name — dish — now carries all four facts together. They can never drift apart againPoshtik Campus
The dish object prints in the Console (F12).
PLAIN HTML — NO CSS AND NO SCRIPT YETdish.name onto the visible page.Each labelled value inside the braces is called a property — a key (the label, e.g. name) paired with a value (e.g. "Millet Khichdi"). An object is simply a bundle of key–value pairs. We used const dish because the box itself won't be re-pointed to a different object — even though, as you'll see later, the values inside it can still change.
The shape, exactly
The object literal — { key: value }, pairs separated by commas.
"Literal" just means you write the object out in full, right there in the code, between curly braces. Learn these five rules once and you can read or write any object: (1) wrap everything in curly braces { }; (2) each entry is a key, then a colon, then a value; (3) separate entries with commas; (4) keys are labels (usually plain words, no quotes needed); (5) values can be any type you already know — string, number, boolean.
| Piece | What it is | In our dish object |
|---|---|---|
| { } | the object's outer wrapper | holds all the dish's facts as one unit |
| key | the label for one fact | name, price, category, isMillet |
| : | the colon — "this key holds this value" | name: "Millet Khichdi" |
| value | the actual data (any type) | "Millet Khichdi", 90, true |
| , | the comma — separates pairs | after every pair except (optionally) the last |
Build one dish object, one line per press — page included, from scratch
Same unbreakable order: plain HTML → CSS → JavaScript. First the page that carries the script (plain, then three CSS rules land one press at a time), then the object literal itself. As the object closes, the Console prints the finished dish — proof the braces built a real value. Notice the keys stay fixed while the values are the three data types from Class 14: a string, a number, a boolean.
<!-- FILE 1 of 3 · literal.html — plain HTML, nothing styled yet --><!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Object literal</title></head><body> <h1>Object literal syntax</h1> <p class="tip">Open the Console (F12) to watch the object build.</p></body></html>/* HTML complete & PLAIN above. FILE 2 of 3 · the CSS — one rule per press ↓ */ <style> body { font-family: Georgia, serif; padding: 18px; } h1 { color: #7C3AED; font-size: 20px; } .tip { color: #64748B; font-size: 13px; } </style><!-- CSS done. Hire the passenger — last line before </body> --> <script src="script.js"></script>// ————— FILE 3 of 3 · script.js — one dish, as an object literal —————const dish = { // open the object name: "Millet Khichdi", // string value price: 90, // number value category: "bowl", // string value isMillet: true // boolean — no comma, it's last}; // close the objectconsole.log(dish);keys are the fixed labels; values are ordinary data. The last pair can skip its comma. Curly braces make an OBJECT — square brackets [ ] would make an array (Class 17)Object literal syntax
Open the Console (F12) to watch the object build.
PLAIN HTML — NO CSS AND NO SCRIPT YETA second, unrelated example — the same shape, a different world
The object shape isn't just for food. Here's a throwaway example modelling a library book — completely different data, exactly the same { key: value } rules. And it gets its own page built from scratch, because a .js file still cannot run by itself: plain HTML → CSS → JS, every time.
<!-- FILE 1 of 3 · book.html — plain HTML first, always --><!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Library book</title></head><body> <h1>Campus Library</h1> <p class="tip">The book object prints in the Console (F12).</p></body></html>/* HTML complete & PLAIN above. FILE 2 of 3 · the CSS — one rule per press ↓ */ <style> body { font-family: Georgia, serif; padding: 18px; } h1 { color: #B45309; font-size: 20px; } .tip { color: #64748B; font-size: 13px; } </style><!-- CSS done. Hire the passenger — last line before </body> --> <script src="book.js"></script>// ————— FILE 3 of 3 · book.js — same rules, a different world —————// same object rules, a totally different thingconst book = { title: "Wings of Fire", author: "A.P.J. Abdul Kalam", pages: 180, available: false};console.log(book);string, string, number, boolean — the values differ, but the object's shape is identical to the dish's. Learn the shape once, reuse it everywhereCampus Library
The book object prints in the Console (F12).
PLAIN HTML — NO CSS AND NO SCRIPT YETProve it — build a real object right here
This console genuinely runs in your browser. Type an object literal, or tap a quick-fill button, and press Enter — JavaScript builds the real object and prints it back. Try changing a value and re-running.
In the console we wrap the object in ( ) — like ({ name: "Veg Samosa" }). Without them, a leading { looks to JavaScript like the start of a code block, not an object. The parentheses say "this is a value, an object literal". Inside a normal const dish = { … } statement you never need them — the = already makes it clear a value is coming.
Reach inside the container
Two ways to read one property — dot . and bracket [ ].
Building the object is half the job; the other half is reading a fact back out. JavaScript gives you two notations, and they return the exact same value: dot notation — dish.price — clean and readable, the one you'll use 95% of the time; and bracket notation — dish["price"] — where the key goes in quotes inside square brackets. Both hand you 90.
| Notation | Looks like | Reach for it when… |
|---|---|---|
| dot | dish.price | you know the key name as you type — the everyday choice. Cleaner, shorter, easier to read. |
| bracket | dish["price"] | the key is stored in a variable, or has spaces/special characters. e.g. dish[chosenKey]. |
See both notations return the same value — click a key, live
This is real: a genuine object dish lives in the page. Click any key below and watch both dish.key and dish["key"] read the same real value out of it. No faked strings — the widget actually reads the object.
const dish = { name: "Millet Khichdi", price: 90, category: "bowl", isMillet: true };
dish.name and dish["name"] return the same value: "Millet Khichdi".dish.price works, but dish["price"] needs the quotes — dish[price] without quotes would look for a variable called price, not the key. That's exactly why bracket notation is the tool for keys-held-in-variables.In code, side by side — built from scratch, HTML → CSS → JS
The page first (plain, then two CSS rules), then the script. The script reads the same two facts twice — once with dots, once with brackets — and the Console proves the pairs are identical.
<!-- FILE 1 of 3 · access.html — plain HTML first --><!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Dot vs bracket</title></head><body> <h1>Reading properties</h1> <p class="tip">Both notations print in the Console (F12).</p></body></html>/* HTML complete & PLAIN above. FILE 2 of 3 · the CSS — one rule per press ↓ */ <style> body { font-family: Georgia, serif; padding: 18px; } h1 { color: #0E7490; font-size: 20px; } .tip { color: #64748B; font-size: 13px; } </style><!-- CSS done. Hire the passenger — last line before </body> --> <script src="script.js"></script>// ————— FILE 3 of 3 · script.js — dot and bracket, same object —————const dish = { name: "Millet Khichdi", price: 90 };// dot notationconsole.log(dish.name); // Millet Khichdiconsole.log(dish.price); // 90// bracket notation — same valuesconsole.log(dish["name"]); // Millet Khichdiconsole.log(dish["price"]); // 90Reading properties
Both notations print in the Console (F12).
PLAIN HTML — NO CSS AND NO SCRIPT YETExam drill · straight from a past paper
"Add a city property to this object — two ways."
This is a real 2-mark question. It gives you an object and asks you to add a new property to it — once with dot notation, once with bracket notation. It's the mirror image of reading a property: instead of = pulling a value out, you put a value in. Reveal the model answer one point at a time.
2 ways = 2 marksQ4. Given let user = {name:'Karan', age:21}; — add a property city with value 'Hyderabad' using both dot and bracket notation. (2 M)
user.city = 'Hyderabad'; — name the new key straight after the dot and assign it. ✓ 1 muser['city'] = 'Hyderabad'; — the key goes in quotes inside brackets. ✓ 1 muser becomes {name:'Karan', age:21, city:'Hyderabad'}.Notice this uses let user, not const — but we still add a property. Adding or changing a property inside an object is allowed even with const; what const forbids is re-pointing the whole variable to a different object. So const user = {…}; user.city = 'Hyderabad'; is perfectly legal. The exam happened to use let, but either works here.
Turn a written spec into an object literal.
Here is a plain-English spec for one dish. Your job: write it as a single object literal, picking the right data type for each fact. Attempt it in your own fswd-practice\Class-15\script.js first — then open the solution and check line by line.
dish"Paneer Tikka"120 (rupees — no quotes, it's a number)"starter"false (it is not a millet dish)Write your object first — even a rough attempt teaches more than reading the answer cold.
<!-- FILE 1 of 3 · activity.html — plain HTML first, as always --><!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Class 15 activity</title></head><body> <h1>Paneer Tikka</h1> <p class="tip">The object prints in the Console (F12).</p></body></html>/* HTML complete & PLAIN above. FILE 2 of 3 · the CSS — one rule per press ↓ */ <style> body { font-family: Georgia, serif; padding: 18px; } h1 { color: #BE185D; font-size: 20px; } .tip { color: #64748B; font-size: 13px; } </style><!-- CSS done. Hire the passenger — last line before </body> --> <script src="script.js"></script>// ————— FILE 3 of 3 · script.js — the object the task asked for —————const dish = { name: "Paneer Tikka", // string price: 120, // number — no quotes category: "starter", // string isMillet: false // boolean — last, no comma};console.log(dish);console.log(dish.price);Paneer Tikka
The object prints in the Console (F12).
PLAIN HTML — NO CSS AND NO SCRIPT YET"120") with no red.Marking your own work: did you quote the strings but leave 120 and false unquoted? Did the last pair skip its comma (or keep it — both are fine)? Did dish.price print 120, not "120"? If yes to all, your object is spec-perfect.
A value can be an action
A property can hold a function. When it does, we call it a method.
So far every value in our object has been data — a string, a number, a boolean. But a value can also be a function. A function stored as a property is called a method: an action the object can do, not just a fact it holds. Our dish can now not only have a name and price — it can describe itself.
Meet this — at the minimum depth you need today. Inside a method, the keyword this means "the object this method belongs to". So inside dish's method, this.name means dish.name. That's all you need for this course — the deeper rules about how this binds in other situations are out of syllabus scope, so we deliberately stop here.
A dish that can describe itself — built from scratch, HTML → CSS → JS
Page first (plain, then three CSS rules one press at a time), then the script. The object gets a method called describe. Calling it with dish.describe() (note the parentheses — that's how you run a function) makes the Console print a sentence built from the object's own properties, via this.
<!-- FILE 1 of 3 · method.html — plain HTML first --><!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Objects with methods</title></head><body> <h1>A dish that describes itself</h1> <p class="tip">The method's sentence appears in the Console (F12).</p></body></html>/* HTML complete & PLAIN above. FILE 2 of 3 · the CSS — one rule per press ↓ */ <style> body { font-family: Georgia, serif; padding: 18px; } h1 { color: #047857; font-size: 20px; } .tip { color: #64748B; font-size: 13px; } </style><!-- CSS done. Hire the passenger — last line before </body> --> <script src="script.js"></script>// ————— FILE 3 of 3 · script.js — an object with a method —————const dish = { name: "Millet Khichdi", price: 90, // a property whose value is a FUNCTION = a method describe: function() { return `${this.name} costs Rs.${this.price}`; }};console.log(dish.describe()); // call it with ()this.name and this.price reach back into the SAME object. dish.describe() RUNS the method; dish.describe (no parens) would just print the function itselfA dish that describes itself
The method's sentence appears in the Console (F12).
PLAIN HTML — NO CSS AND NO SCRIPT YETthis built, from the object's own two properties() — that's the whole difference between owning a recipe and cooking it.this AND NOT JUST name?Inside the method you must write this.name, not bare name. Bare name would look for a variable called name in the surrounding code — not the object's property. this. is the bridge from "inside the method" back to "the object I live on". Change dish.name later and describe() automatically reflects it, because it reads the property fresh every time it runs.
Read the object and the console.log lines — predict every printed value.
Below is an object and five console.log lines. Cover the right side, read each line, and write down what you think it prints — before you reveal. This is the single best drill for objects: it forces you to trace dot access, bracket access, a method call, and a missing key.
const car = { brand: "Tata", seats: 5, isElectric: true, honk: function(){ return "Beep!"; } };console.log(car.brand);console.log(car["seats"]);console.log(car.isElectric);console.log(car.honk());console.log(car.colour); ← careful — this key was never setCommit to all five predictions first — especially line 5.
<!-- FILE 1 of 3 · predict.html — the page that runs the five lines --><!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Predict the output</title></head><body> <h1>Tata — car object</h1> <p class="tip">All five answers appear in the Console (F12).</p></body></html>/* HTML complete & PLAIN above. FILE 2 of 3 · the CSS — one rule per press ↓ */ <style> body { font-family: Georgia, serif; padding: 18px; } h1 { color: #1D4ED8; font-size: 20px; } .tip { color: #64748B; font-size: 13px; } </style><!-- CSS done. Hire the passenger — last line before </body> --> <script src="script.js"></script>// ————— FILE 3 of 3 · script.js — the five lines you predicted —————const car = { brand: "Tata", seats: 5, isElectric: true, honk: function(){ return "Beep!"; } };console.log(car.brand); // dotconsole.log(car["seats"]); // bracketconsole.log(car.isElectric); // booleanconsole.log(car.honk()); // method callconsole.log(car.colour); // missing key!Tata — car object
All five answers appear in the Console (F12).
PLAIN HTML — NO CSS AND NO SCRIPT YETconsole.log only ever writes to the second surface.Line 5 is the trap. Reading a key that was never set doesn't crash — JavaScript quietly returns undefined. That's very different from a typo in a method call or reading a property of undefined, which do throw. Remember: missing key → undefined, no error. And car.honk() needs its parentheses — without them you'd print the function itself, not "Beep!".
A value can be another object
Nested objects — an object living inside a property of another object.
You've seen values that are strings, numbers, booleans, and functions. A value can also be a whole other object. When a dish's nutrition is itself a bundle of facts — protein, calories — it makes sense to store it as its own little object, tucked inside the dish. To read a fact two levels deep, you just chain the dots: dish.nutrition.protein.
A dish with a nutrition sub-object — built from scratch, then stepped through
Same rule as every JavaScript demo in this course: a .js file cannot be double-clicked into life, so we build its host page first — plain HTML, then a little CSS, then the script. Then step through and watch the Console. The outer object dish holds a normal name and price — and a nutrition property whose value is a second object. Reading into it is just dots all the way down.
<!-- FILE 1 of 3 · nested.html — the page that hosts the nested object --><!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Nested objects</title></head><body> <h1>Millet Khichdi — nutrition</h1> <p class="tip">Open the Console (F12) to see the sub-object.</p></body></html>/* HTML complete & PLAIN above. FILE 2 of 3 · the CSS — one rule per press ↓ */ <style> body { font-family: Georgia, serif; padding: 18px; } h1 { color: #0F766E; font-size: 20px; } .tip { color: #64748B; font-size: 13px; } </style><!-- CSS done. Hire the passenger — last line before </body> --> <script src="script.js"></script>// ————— FILE 3 of 3 · script.js — an object inside an object —————const dish = { name: "Millet Khichdi", price: 90, nutrition: { // a whole object as a value protein: 12, // grams calories: 320 }};console.log(dish.nutrition); // the sub-objectconsole.log(dish.nutrition.protein); // two levels deepchain the dots: dish → nutrition → protein. Each dot steps one level in. Bracket works too: dish["nutrition"]["protein"]Millet Khichdi — nutrition
Open the Console (F12) to see the sub-object.
PLAIN HTML — NO CSS AND NO SCRIPT YET<h1> and the tip you typed, while the Console below shows the object inside the object. One dot reached nutrition; a second dot reached protein.dish.nutrition.protein is fine because nutrition exists. But dish.allergens.count would throw Cannot read properties of undefined — because dish.allergens is undefined, and you can't read .count off undefined. Reading one missing key is safe (gives undefined); reading into a missing key crashes. You'll meet the safe fix (optional chaining ?.) in a later class.
Self-study · a light preview
Object or array? A quick-decision reference.
You'll meet arrays in full next class (Class 17), but you'll already sense the choice: an object stores facts you look up by name (dish.price); an array stores a list of items you look up by position (menu[0]). This is a self-study card — skim it now, and it'll click fully once arrays land.
| Question | Object { } | Array [ ] |
|---|---|---|
| Shape | named labels → values | an ordered list of items |
| Look up by… | name — dish.price | position — menu[0] |
| Best for | one thing with many facts (a dish, a user) | many things of the same kind (a whole menu) |
| Order matters? | no — keys aren't positional | yes — item 0, item 1, item 2… |
| Example | { name:"Khichdi", price:90 } | ["Khichdi", "Samosa", "Chai"] |
The rule of thumb: if you'd describe the data as "a list of…", reach for an array. If you'd describe it as "one thing with a name, a price, a…", reach for an object. And the two combine beautifully — a whole menu is naturally an array of objects: [ {name:"Khichdi",price:90}, {name:"Samosa",price:20} ]. That exact combination is the first thing you'll build in Lab 4.
Put it all together — a real console
Practise everything from today in a genuine JavaScript console.
This console truly runs in your browser — build an object, read it with dot and bracket, call a method, add a property, read a missing key. Tap a quick-fill to see each idea from today prove itself, then type your own. Copy any of these into your fswd-practice\Class-15\script.js and run them with F12 → Console.
Two traps that catch everyone — read the real errors
These are the two mistakes you'll make in your first hour with objects. Read the exact console messages so you recognise them instantly.
script.js:3
undefined throws Cannot read properties of undefined — because dish.nutrition doesn't exist, so .protein has nothing to read from. Fix: make sure the sub-object exists first.Wrap-up & the bridge to Class 16
Your data now has shape. Next class, JavaScript reaches out and changes the page itself.
Today one box learned to hold many labelled facts — a dish's name, price, category, millet-flag — read two ways, given a method, and nested one object inside another. But an object still only lives in memory. Class 16 introduces the DOM: the browser's live model of your HTML, so JavaScript can find an element on the page and change its text, its style, anything. Objects are the data; the DOM is the page. Lab 4 puts both to work together.
poshtik-campus\ project during the labs, where the continuous build is what actually gets committed.Take-home kit — the object cheat-card
Copy this into your notes. It answers every object exam question you saw today in a single glance.
- Object literal:
const dish = { name: "Khichdi", price: 90 };—{ key: value }pairs, comma-separated. - Read two ways: dot
dish.price(everyday) and bracketdish["price"](when the key is in a variable). Same value. - Add / change:
dish.city = "Hyderabad"creates the key if missing — allowed even on aconstobject (P2·Q4). - Method: a property whose value is a function; inside it,
this.namemeans "this object's name". Call it withdish.describe(). - Nested + missing keys:
dish.nutrition.proteinchains dots; a missing key reads asundefined(safe), but reading into undefined throws.
- Write an object literal for a dish with a name, a price, and an isMillet flag.
- Read that dish's price two ways — dot and bracket notation.
- Given
let user={name:'Karan',age:21}, addcity:'Hyderabad'both ways (P2·Q4). - What is a method, and what does
thismean inside one? - What does reading a key that was never set return — and when does reading a nested key crash instead?