JavaScript Flashcards
What are the data types in JavaScript?
JavaScript has several data types, including Primitive types (String, Number, Boolean, Null, Undefined, Symbol, BigInt) and Reference types (Objects, Arrays, Functions).
What is the difference between let
, const
, and var
?
var
is function-scoped or globally scoped and can be re-declared and updated. let
is block-scoped, can be updated but not re-declared, while const
is block-scoped, cannot be updated or re-declared, and is used for constants.
What is a closure in JavaScript?
A closure is a function that retains access to its lexical scope, even when executed outside that scope, commonly used for data encapsulation.
Explain the concept of hoisting.
Hoisting is JavaScript’s behavior of moving variable and function declarations to the top of their containing scope during the compile phase, allowing references before declarations.
What are promises, and how do they work?
A promise is an object representing the eventual completion or failure of an asynchronous operation, with three states: pending, fulfilled, or rejected, and handled using .then()
and .catch()
.
What is the difference between ==
and ===
?
==
performs type coercion, converting operands to the same type for comparison, while ===
is a strict equality comparison that checks both value and type without coercion.
What is event delegation?
Event delegation is a technique where a single event listener is attached to a parent element to manage events for multiple child elements, improving efficiency.
What is the difference between null
and undefined
?
null
is a deliberate assignment of ‘no value’ to a variable, while undefined
indicates a variable that has been declared but not assigned a value.
What are arrow functions, and how do they differ from regular functions?
Arrow functions provide a shorter syntax for writing functions and do not have their own this
, inheriting it from the enclosing lexical context.
Explain the concept of the ‘this’ keyword in JavaScript.
The this
keyword refers to the context in which a function is called, changing its value based on the invocation method (e.g., method call, function call).
What are template literals?
Template literals are string literals that allow embedded expressions, defined using backticks (``), with placeholders indicated by ${expression}
.
What is the event loop in JavaScript?
The event loop is a mechanism that allows JavaScript to perform non-blocking operations by continuously checking the call stack and message queue, executing functions from the stack and processing queued messages.
Explain how map
, filter
, and reduce
work.
map
creates a new array populated with results from a provided function on every element. filter
creates a new array with all elements that pass a test implemented by a provided function. reduce
executes a reducer function on each element, resulting in a single output value.
What is the purpose of the bind
, call
, and apply
methods?
These methods change the this
context of a function: call
calls a function with a given this
value and arguments provided individually; apply
calls a function with a given this
value and arguments as an array; bind
returns a new function permanently bound to a specific this
value and initial arguments.
What is the difference between synchronous and asynchronous programming?
Synchronous: Code is executed sequentially, blocking subsequent code until the current operation completes. Asynchronous: Code can be executed in parallel, allowing other operations to run without waiting for the current operation to finish.