JavaScript Flashcards
(60 cards)
What is the difference between var
, let
, and const
in JavaScript?
var
is function-scoped and can be redeclared; let
is block-scoped and can be reassigned; const
is block-scoped and cannot be reassigned, but its value (if an object) can be mutated.
What does this
refer to in JavaScript?
this
refers to the context in which a function is called. In global scope, it’s the window
object (browser) or global
(Node.js). In an object method, it refers to the object.
What is a closure in JavaScript?
A closure is a function that retains access to its outer scope’s variables even after the outer function has finished executing.
What is the event loop in JavaScript?
The event loop is a mechanism that handles asynchronous tasks by processing the call stack and pushing tasks from the callback queue to the stack when it’s empty, enabling non-blocking I/O.
What is the difference between ==
and ===
in JavaScript?
==
performs type coercion before comparison, while ===
checks for both value and type equality.
How does prototypal inheritance work in JavaScript?
Objects inherit properties and methods from a prototype object. Each object has a \_\_proto\_\_
property linking to its prototype.
What is a Promise in JavaScript?
A Promise is an object representing the eventual completion or failure of an asynchronous operation. It has states (pending
, fulfilled
, rejected
) and methods like .then()
, .catch()
, and .finally()
.
Explain async
and await
in JavaScript.
async
declares a function that returns a Promise. await
pauses execution inside an async
function until a Promise resolves.
What is the difference between null
and undefined
?
null
is an explicit assignment indicating no value, while undefined
means a variable is declared but not assigned.
What is event delegation in JavaScript?
Event delegation uses event bubbling to handle events on a parent element for its children, reducing the number of event listeners.
What are arrow functions, and how do they differ from regular functions?
Arrow functions (=>
) have a concise syntax and lexically bind this
to the enclosing context, unlike regular functions.
What is the purpose of Object.defineProperty
?
Object.defineProperty
defines or modifies a property on an object, allowing control over its value, writability, enumerability, and configurability.
Explain the concept of hoisting in JavaScript.
Hoisting moves variable and function declarations to the top of their scope during compilation. var
variables are hoisted and initialized as undefined
; let
and const
are hoisted but not initialized.
What is a generator function in JavaScript?
A generator function (declared with function*
) returns an iterator and can pause execution with yield
.
What is the difference between map
, filter
, and reduce
?
map
transforms each element into a new array; filter
creates an array with elements passing a test; reduce
accumulates elements into a single value.
How does try...catch
work in JavaScript?
try...catch
handles errors: code in the try
block is executed, and if an error occurs, control moves to the catch
block.
What is a WeakMap in JavaScript?
A WeakMap
is a collection where keys must be objects, and entries are weakly referenced, allowing garbage collection if the key is no longer referenced.
Explain the bind
method.
The bind
method creates a new function with a fixed this
value and optional preset arguments.
What is memoization, and how can it be implemented in JavaScript?
Memoization caches function results for given inputs to improve performance.
What are JavaScript modules, and how do they work?
Modules encapsulate code using import
and export
syntax, enabling reusable, scoped code.
What is currying in JavaScript, and how is it implemented?
Currying transforms a function with multiple arguments into a sequence of functions, each taking one argument. Example: const curry = (fn) => (a) => (b) => fn(a, b); const add = curry((a, b) => a + b); add(2)(3); // 5
.
How does this
behave in an arrow function compared to a regular function?
In arrow functions, this
is lexically bound to the enclosing scope’s this
value, while in regular functions, this
depends on how the function is called (e.g., obj.method()
, call
, or apply
).
What is the purpose of the class
syntax in JavaScript?
The class
syntax is syntactic sugar for constructor functions, providing a cleaner way to create objects with prototypes for inheritance. Example: class MyClass { constructor() {} }
.
How does the super
keyword work in JavaScript classes?
super
calls the parent class’s constructor or methods in a subclass. Example: class Child extends Parent { constructor() { super(); } }
invokes the Parent
constructor.