Courses / HEC NSCT – National Skill Competency Test: Complete Preparation for Computing Graduates
6.3 JavaScript and the DOM
Area 6 – Web Development (10%) · 50 min read · 25 MCQs to check yourself
JavaScript basics
- Variables:
let(block scoped, reassignable),const(block scoped, cannot be reassigned),var(function scoped, hoisted – avoid). - Types: number, string, boolean, null, undefined, object, symbol, bigint.
typeof nullis"object"(a famous quirk). ==compares after type conversion ("5" == 5is true);===compares value and type (strict – use it).- Functions: declarations, arrow functions
(a, b) => a + b, callbacks, closures (inner function remembers outer variables). - Arrays:
map,filter,reduce,forEach,find,push,pop. Objects use key–value pairs; JSON is a text format for them (JSON.stringify,JSON.parse).
The DOM
The Document Object Model is the tree of objects that represents the page.
const btn = document.getElementById("save");
btn.addEventListener("click", () => {
document.querySelector(".msg").textContent = "Saved!";
});
- Select:
getElementById,querySelector,querySelectorAll. - Change:
textContent,innerHTML(careful – XSS risk),classList.add,style. - Events bubble from the target up to its ancestors (event bubbling);
event.preventDefault()stops default actions (e.g. form submit).
Asynchronous JavaScript
JavaScript is single-threaded with an event loop. Long tasks run asynchronously: callbacks → Promises → async/await.
async function loadUser() {
const res = await fetch("/api/users/1");
const user = await res.json();
}
AJAX / fetch loads data without reloading the page.
The JavaScript DOM explained in 5 minutes (Bro Code)
This lesson ends with a quiz of 25 MCQs. Open the lesson on PrepOasis to take it and save your progress.
Previous: 6.2 HTML and CSS Essentials · Next: 6.4 Frameworks, Back-End and REST APIs