JavaScript Questions Flashcards
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.
What is a callback function, and how is it used in JavaScript?
A callback function is a function passed as an argument to another function, which is executed after the completion of some operation (typically asynchronous).
What is an IIFE (Immediately Invoked Function Expression), and when would you use it?
An IIFE is a function that is defined and invoked immediately after its creation. It’s often used to create a private scope, avoiding polluting the global namespace.
What are higher-order functions in JavaScript?
A higher-order function is a function that takes one or more functions as arguments or returns a function as a result.
What is the difference between null and undefined in JavaScript?
undefined: A variable that has been declared but not assigned a value is undefined.
null: An explicit assignment indicating “no value” or “empty value.”
What are arrow functions, and how do they differ from regular functions?
Arrow functions have a more concise syntax and lexical scoping for this, meaning they inherit this from the surrounding context.
What is the spread operator (…) in JavaScript, and how is it used?
The spread operator allows you to unpack values from arrays or objects. It’s used to copy, merge, or spread elements.
What is destructuring in JavaScript?
Destructuring allows you to unpack values from arrays or properties from objects into distinct variables.
What is the bind() method in JavaScript?
The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided to the function when it’s invoked.
What are template literals in JavaScript?
Template literals (or template strings) allow for multi-line strings and embedding expressions inside strings.
What is the setTimeout() function, and how is it used?
setTimeout() is used to execute a function or a piece of code after a specified delay (in milliseconds).
What is JSON.stringify() and JSON.parse()?
JSON.stringify(): Converts a JavaScript object into a JSON string.
JSON.parse(): Converts a JSON string into a JavaScript object.
What are async/await in JavaScript?
async/await is a modern syntax for working with promises. async is used to declare an asynchronous function, and await is used inside the function to pause execution until a promise is resolved
What is the difference between synchronous and asynchronous code in JavaScript?
Synchronous code: Executes one line after another, blocking further execution until the current task completes.
Asynchronous code: Executes tasks independently, allowing other tasks to run while waiting for an operation (like fetching data).