Store data in arrays and objects, then control the web page: change it, listen to clicks, check forms and build lists.
Every example opens in a live browser preview. Click ✎ Edit on any preview, or press L, to open the Code Lab.
| Method | Returns |
|---|---|
includes(x) | true / false |
indexOf(x) | position, or -1 |
find(fn) | first item that passes |
some / every | true / false |
{ name: ..., price: ... } is an object – the next module explains them. Real app data is almost always an array of objects.[...arr] (spread) if you need the original.| Method | Use for |
|---|---|
join() | array → text (tags, CSV) |
slice(a, b) | take a part (top 3, page 2) |
[...a, ...b] | merge or copy |
Array.from | make number lists |
slice. Count distinctions with filter. Add 5 grace marks to everyone with map.reduceMath.max(...marks)filterMath.max(...marks): the spread operator passes every item as a separate argument.Module 8: Objects & JSON – describe a student, a product or a user with properties.
Q1. Which method makes a NEW array with every item changed?
Q2. [5, 12, 8].filter(n => n > 6) gives:
Q3. [10, 9, 100].sort() gives:
| Array | Object |
|---|---|
| A list of many things | Details of one thing |
[ ], accessed by index | { }, accessed by key name |
marks[0] | student.name |
| Order matters | Keys have names, order rarely matters |
{ from, text, time, read }. The chat is an array of these objects.this means "the object that owns this method". account.deposit() → this = account.console.log, "abc".toUpperCase(), arr.push() are all methods on objects.| Method | Gives |
|---|---|
Object.keys(o) | array of keys |
Object.values(o) | array of values |
Object.entries(o) | array of [key, value] pairs |
for (k in o) | each key |
const { name, price } = props and { ...state, count: 1 }.| JSON rule | |
|---|---|
| Keys in double quotes | {"name": "Riya"} |
| Strings in double quotes only | no single quotes |
| No functions, no comments | data only |
| No trailing commas | [1, 2,] is invalid |
isOnline property and show a green dot when true. Make an array of 2 users and render both.class is a reserved word, so read it with student.class instead of destructuring.(obj, null, 2) shown in a <pre>Module 9: The DOM – use JavaScript to find and change any part of a web page.
Q1. How do you read the city of const u = {city: "Ranchi"}?
Q2. JSON.stringify turns an object into:
Q3. Inside a method, this refers to:
document is the top of the tree – your entry pointquerySelector uses the same selectors you learnt in CSS: #id, .class, ul li, :last-child.| Method | Returns | Example | Use when |
|---|---|---|---|
getElementById("x") | one element or null | getElementById("cart") | Element has an id (fastest) |
querySelector(css) | FIRST match or null | querySelector(".price") | Any CSS selector, one element |
querySelectorAll(css) | NodeList (all matches) | querySelectorAll(".card") | Many elements; loop with forEach |
getElementsByClassName | live HTMLCollection | (older style) | You will see it in old code |
null. Then null.textContent throws TypeError: Cannot read properties of null. Check the spelling of the id/class, and that the script runs after the HTML.const btn = document.querySelector("#orderBtn"); – then use btn many times instead of searching again.| Method | Does |
|---|---|
el.classList.add("x") | add a class |
el.classList.remove("x") | remove it |
el.classList.toggle("x") | add if missing, remove if present |
el.classList.contains("x") | true / false |
.dark { ... }) and let JS only switch classes on/off. Cleaner code, and designers can change the look without touching JS.el.closest(".card") finds the nearest parent matching a selector. Very useful with buttons inside cards (Module 10).images/netarhat.jpg and the title to match. Change the button text to "Show less" when open. Add a class instead of setting style.color.body.dark styles for background and textdocument.body.classList.toggle("dark")contains("dark")<img id="main"> and a captiononclick="show(this)"dataset.price from the elementdata-price="199" on HTML is read in JS as element.dataset.price (a string).Module 10: Events – respond to clicks, typing, keys and scrolling.
Q1. Which selects ALL elements with class "card"?
Q2. Safest way to show user-typed text?
Q3. Best way to switch dark mode?
| Way | Example | Use? |
|---|---|---|
| HTML attribute | onclick="like()" | quick demos |
| Property | btn.onclick = like | only one handler |
| addEventListener | btn.addEventListener("click", like) | best: many handlers, more control |
| Event | Fires when |
|---|---|
click / dblclick | clicked / double-clicked |
mouseenter / mouseleave | mouse moves in / out |
input | every change while typing |
change | value committed (select, checkbox) |
keydown | a key is pressed |
focus / blur | box selected / left |
submit | a form is sent |
scroll, resize, load | window events |
| Property | Gives |
|---|---|
e.target | the element that was clicked/typed in |
e.type | "click", "keydown"... |
e.key | key pressed: "Enter", "a", "ArrowUp" |
e.target.value | text in an input |
e.stopPropagation() stops the bubble. Needed rarely, for example a button inside a clickable card.| Element | Default action |
|---|---|
link <a> | opens the URL |
<form> submit | reloads / goes to action URL |
| right-click | context menu |
| checkbox | toggles |
e.preventDefault() and sends the data with JavaScript (Module 16).input eventdata-tab="overview" etc.nextElementSiblingkeydown on documentstyle.transformscroll on windowscrollY > 200window.scrollTo({ top: 0, behavior: "smooth" })Module 11: Forms & Validation with JS – check data before it is sent.
Q1. Which event fires on every keystroke in an input?
Q2. e.target is:
Q3. Stop a form from reloading the page:
| Field | Read with |
|---|---|
| text, email, number | input.value (always a string) |
| number as number | Number(input.value) or input.valueAsNumber |
| checkbox | input.checked (true/false) |
| radio group | form.querySelector("[name=batch]:checked").value |
| select | select.value |
| whole form | new FormData(form) |
form.name, form.age give the field with that name attribute.required, type="email", pattern: quick, built in (Part 1 of HTML course).novalidate attribute turns off the browser bubbles so your own messages show instead.| Pattern | Means |
|---|---|
\d | any digit 0-9 |
[a-z] | one letter a to z |
{6} / {2,4} | exactly 6 / between 2 and 4 |
+ / * | one or more / zero or more |
^ ... $ | the WHOLE text must match |
\s | a space |
/i /g | ignore case / all matches |
ranchi → ranchi2026 → Ranchi2026 → Ranchi@2026{ "834001": "Ranchi", ... } instead of an array.novalidate form + submit listener with preventDefaultsetError(field, message)includes (lowercase)checkValidity()Module 12: Creating Elements & Rendering Lists – build the page from data.
Q1. A checkbox is ticked when:
Q2. /^\d{6}$/ matches:
Q3. Is JavaScript validation enough for security?
| Method | Does |
|---|---|
createElement("li") | makes a new element (not on page yet) |
parent.append(el) | adds at the end |
parent.prepend(el) | adds at the start |
el.remove() | deletes it |
| createElement | innerHTML template | |
|---|---|---|
| Code length | longer | short |
| Safe with user text? | yes (textContent) | no, unless escaped |
| Keeps existing listeners? | yes | no (rebuilds everything) |
| Best for | adding one item, user content | rendering a whole list from your data |
products (never changed)filter for search + categorysort for price ordermap to HTML, join, put into the gridsetTimeout (preview of Module 13). Show the time under each message.todos in localStorage so tasks survive a refresh.new Set(array) removes duplicates: perfect for building a category list from data.Part 3: Timers, localStorage, async JavaScript and real APIs.
Q1. Which adds an element at the END of a list?
Q2. In the render pattern, to delete an item you:
Q3. [...new Set(["a","b","a"])] gives:
You have finished Part 2 of the Complete JavaScript Course.
Keep all your practice files in D:\js-practice. You will reuse them in the capstone apps.