JavaScript Flashcards
(300 cards)
What is a closure in JavaScript?
A closure is a function that retains access to its lexical scope even when the function is executed outside that scope.
Why are closures useful?
They allow functions to have private variables and enable powerful patterns like function factories and partial application.
What is hoisting in JavaScript?
Hoisting is JavaScript’s default behavior of moving declarations to the top of the current scope.
What gets hoisted?
var
and function declarations are hoisted. let
and const
are hoisted but not initialized.
What is lexical scope?
Lexical scope refers to the fact that a function’s scope is determined by its position in the source code.
What does the this
keyword refer to?
this
refers to the object that is executing the current function.
How does this
behave in arrow functions?
Arrow functions inherit this
from their lexical scope; they do not bind their own this
.
What is the event loop?
The event loop is the mechanism that handles asynchronous callbacks in JavaScript by dequeuing tasks from the event queue when the call stack is clear.
What is the call stack?
The call stack is a data structure that keeps track of function calls in JavaScript.
What is the task queue?
A queue that holds callback functions to be executed after the current call stack is empty.
What is a promise in JavaScript?
A Promise is an object representing the eventual completion or failure of an asynchronous operation.
What are the states of a promise?
Pending, Fulfilled, and Rejected.
What does async/await
do?
It allows writing asynchronous code that looks synchronous, by pausing execution until a Promise resolves.
What is prototypal inheritance?
A mechanism by which an object can inherit properties and methods from another object via its prototype.
What is the difference between \_\_proto\_\_
and prototype
?
\_\_proto\_\_
is the actual object used in the prototype chain; prototype
is a property of constructor functions used to define properties on instances.
What is type coercion in JavaScript?
Type coercion is the automatic or implicit conversion of values from one data type to another.
What is the difference between ==
and ===
?
==
compares values with type coercion; ===
compares values without coercion.
What causes a memory leak in JavaScript?
Memory leaks occur when references to unused objects are unintentionally kept, preventing garbage collection.
How does garbage collection work in JavaScript?
It automatically frees up memory by removing objects that are no longer reachable.
What is the difference between null
and undefined
?
undefined
is a variable declared but not assigned. null
is an intentional absence of any value.
What happens when you access a non-existent property?
undefined
is returned.
Explain a closure in JavaScript?
A closure is a function that retains access to its lexical scope even when the function is executed outside that scope.
Reason why are closures useful?
They allow functions to have private variables and enable powerful patterns like function factories and partial application.
Explain hoisting in JavaScript?
Hoisting is JavaScript’s default behavior of moving declarations to the top of the current scope.