JavaScript Programming
JavaScript Programming covers the language itself plus browser interaction. Questions test value types and coercion, scope, functions, array and object methods, DOM events, and asynchronous code.
Values, scope, and operators
constandletare block-scoped;varis function-scoped and hoisted asundefined.===compares without coercion;==converts types, so"5" == 5is true.- Falsy values are
false,0,"",null,undefined, andNaN.
Functions and objects
- Function declarations hoist; function expressions and arrow functions do not.
- Arrow functions do not bind their own
this. - Closures let a function retain access to variables from its defining scope.
DOM, events, and async
- Use
document.querySelector(),addEventListener(), andevent.preventDefault()to control the page. - A promise is pending, fulfilled, or rejected;
async/awaitmakes asynchronous code read sequentially, andawaitcalls belong intry/catch.
Worked example
const prices = [10, 25, 5];
const total = prices
.filter(p => p > 5)
.map(p => p * 2)
.reduce((sum, p) => sum + p, 0);
Reasoning: filter keeps [10, 25]; map produces [20, 50]; reduce starts from 0 and returns 70. Each method returns a new array except reduce, so prices is unchanged - a common exam point.
Traps
Mutating an object held in a const is allowed, because the binding is constant, not the contents. Remember that NaN !== NaN, that await only works inside an async function, and that this inside a setTimeout callback is not the surrounding object. Forgetting return in an arrow function with braces silently yields undefined.