JavaScript Questions Flashcards
(38 cards)
What is the difference between let and const?
let allows reassignment, while const does not allow reassignment.
const ensures immutability of the variable binding, not the value itself.
What is a callback?
A callback is a function passed as an argument to another function, executed once a task is complete.
When would you use a callback?
When handling asynchronous code, such as fetching data from an API or dealing with user interactions.
What is the difference between == and ===?
== performs loose comparison with type coercion, while === performs strict comparison without type coercion.
What is hoisting?
Hoisting is a JavaScript behavior where variable and function declarations are moved to the top of their containing scope.
What is a closure?
A closure is a function that retains access to its lexical scope even when executed outside of that scope.
What is the event loop?
The event loop is a mechanism in JavaScript that allows non-blocking, asynchronous code execution.
When is it a good idea to use a class?
When you need to create objects that share common behavior or properties, especially in object-oriented programming.
What is the difference between let, const, and var?
var is function-scoped and can be re-declared; let and const are block-scoped. let allows reassignment, const does not.
What is event delegation in JavaScript?
Event delegation is attaching a single event listener to a parent element to handle events from child elements.
What is a closure in JavaScript?
A closure retains access to variables from its lexical scope after that scope has finished executing.
What is the event loop and how does it work?
The event loop checks the call stack and processes tasks in the callback queue when the stack is empty.
Explain JavaScript’s asynchronous programming model.
JavaScript uses Promises and async/await for asynchronous operations, making code look synchronous.
What is the difference between null and undefined?
null represents intentional absence of a value; undefined means a variable is declared but not assigned.
Explain the concept of hoisting in JavaScript.
Hoisting moves variable and function declarations to the top of their scope, but only declarations are hoisted.
What are higher-order functions in JavaScript?
Higher-order functions take other functions as arguments or return functions.
Give an example of a higher-order function.
Example: function sayHi(fn, name) { console.log(fn(name)); }
What is the difference between var, let, and const?
var: Declares a variable with function scope (or global scope if declared outside a function). It is hoisted to the top of its scope and can be re-assigned.
let: Declares a variable with block scope (within curly braces). It is hoisted but not initialized, so it has a “temporal dead zone” until assigned.
const: Declares a variable with block scope that cannot be re-assigned after initialization. It is also hoisted, but it must be initialized at the time of declaration
What are closures in JavaScript, and how do they work?
A closure is a function that retains access to its lexical scope, even when the function is executed outside that scope. Closures are created when a function is defined inside another function and the inner function has access to the outer function’s variables.
What is the event loop in JavaScript, and how does it work?
The event loop is the mechanism that allows JavaScript to execute asynchronous code. JavaScript is single-threaded, and the event loop enables non-blocking I/O operations. When an asynchronous operation (e.g., setTimeout(), network requests) is triggered, the callback is added to the event queue. The event loop continuously checks the call stack and moves items from the event queue to the stack when it’s empty.
What is the difference between == and === in JavaScript?
== (Loose equality): Compares two values for equality, with type coercion. It will try to convert values to the same type before comparison.
=== (Strict equality): Compares both the value and the type without type coercion.
What are promises in JavaScript, and how do you use them?
A Promise represents the eventual completion (or failure) of an asynchronous operation and its resulting value. It has three states: pending, fulfilled, and rejected.
What is the this keyword in JavaScript, and how does it behave in different contexts?
Global scope: this refers to the global object (in browsers, it’s window).
Object method: this refers to the object the method is called on.
Arrow functions: this refers to the lexical scope (it doesn’t create its own this).
Explain the concept of “hoisting” in JavaScript
Hoisting is JavaScript’s behavior of moving declarations (not initializations) to the top of their containing scope during compilation. This applies to both variables and functions.