JavaScript Programming

90 flashcards by MentisLibrary

Types and coercion, closures, prototypes, promises and async, the DOM, and modern ES6+ syntax. Heavy on the surprising behavior that shows up in real code and in interviews.

What are JavaScript's primitive types?
string, number, bigint, boolean, undefined, symbol, and null.
Why does typeof null return 'object'?
A bug baked into the first implementation, where the type tag for objects was zero and null was the null pointer.
Difference between null and undefined
undefined means a variable exists but has no assigned value; null is an explicit assignment meaning no value.
Difference between == and ===
== compares after type coercion; === requires the same type and value with no coercion.
What is type coercion?
Automatic conversion between types when an operation expects a different type.
What are the falsy values?
false, 0, -0, 0n, empty string, null, undefined, and NaN. Everything else is truthy.
What does NaN mean and how do you test for it?
Not a Number, the result of an invalid numeric operation, and the only value not equal to itself.
var versus let versus const
var is function scoped and hoisted as undefined; let and const are block scoped with a temporal dead zone; const forbids reassignment.
What is hoisting?
Declarations are processed before any code runs, so function declarations and var names exist before their line.
What is the temporal dead zone?
The span between entering a scope and the point where a let or const binding is initialized, where touching it throws.
What is a closure?
A function bundled with the variables it captured from the scope where it was defined.
Why does a var loop with setTimeout print the same number?
var is function scoped, so all callbacks share one binding, which holds the final value by the time they run.
What is the difference between a function declaration and a function expression?
A declaration is hoisted in full and can be called before its line; an expression is assigned to a variable and follows normal initialization rules.
How do arrow functions treat this?
They have no own this; they capture it lexically from the enclosing scope at definition time.
What determines the value of this in a normal function?
How the function is called: as a method, this is the receiver; standalone, it is undefined in strict mode; with new, it is the new object.
What do call, apply, and bind do?
call and apply invoke a function with an explicit this (apply takes an array of arguments); bind returns a new function with this permanently fixed.
What is the prototype chain?
Every object has an internal link to a prototype object, and property lookups walk that chain until a match or null.
Difference between __proto__ and prototype
__proto__ is an object's actual prototype link; prototype is a property on constructor functions used as the prototype for instances they create.
What does the class keyword actually create?
Syntactic sugar over constructor functions and prototypes, plus stricter semantics such as no hoisting-as-callable and mandatory new.
What does the new operator do?
Creates an object, links it to the constructor's prototype, runs the constructor with this bound to it, and returns it unless the constructor returns another object.
What is Object.create used for?
Creating a new object with an explicitly chosen prototype, including null for a bare dictionary.
What is a Promise?
An object representing a value that is not available yet, in one of three states: pending, fulfilled, or rejected.
What does then return?
A new promise, which is what makes promises chainable; returning a value from the callback fulfills it, and returning a promise adopts that promise.
How do you handle promise errors?
Attach .catch, or use try and catch around await, and remember that a rejection skips every then until the next catch.
What do async and await do?
async makes a function return a promise; await pauses it until a promise settles, without blocking the thread.
Promise.all versus Promise.allSettled
all rejects as soon as any input rejects; allSettled always fulfills with a status entry for every input.
Promise.race versus Promise.any
race settles with the first promise to settle either way; any fulfills with the first fulfillment and only rejects if all reject.
How do you run async calls in parallel rather than in sequence?
Start all the promises first, then await them together, instead of awaiting inside a loop.
What is the event loop?
The runtime loop that takes a task from the queue, runs it to completion, drains the microtask queue, and repeats.
Microtasks versus macrotasks
Microtasks (promise callbacks, queueMicrotask) run right after the current task and before the next macrotask (setTimeout, I/O, events).