Javascript Interview Deck 5 Flashcards
(33 cards)
What is JavaScript?
JavaScript is a high-level, interpreted programming language primarily used for client-side web development. It enables interactive web pages and is commonly used in conjunction with HTML and CSS.
What are the data types in JavaScript?
JavaScript has several built-in data types, including:
Primitive types: string, number, boolean, null, undefined, symbol.
Non-primitive types: object (including arrays and functions).
Explain the concept of hoisting in JavaScript.
Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their containing scope during compilation, regardless of where they are declared.
What is the difference between == and === in JavaScript?
== is used for loose equality comparison, where type conversion is performed, while === is used for strict equality comparison, where both value and type must be the same.
Explain the event bubbling and event capturing in JavaScript.
Event bubbling is a propagation mechanism in the DOM where events are first captured by the innermost element and then propagated to outer elements. Event capturing is the reverse, where events are captured at the outermost element and then propagated to inner elements.
What are closures in JavaScript?
Closures are functions that have access to the outer (enclosing) function’s variables, even after the outer function has finished executing. They “remember” the environment in which they were created.
What are promises in JavaScript?
Promises are a way to handle asynchronous operations in JavaScript. They represent a value that may be available now, or in the future, or never. Promises allow chaining asynchronous operations and handling success or failure with then() and catch() methods.
What are arrow functions in JavaScript?
Arrow functions are a concise way to write anonymous functions in JavaScript. They have a shorter syntax compared to traditional function expressions and do not bind their own this, arguments, super, or new.target.
What is the difference between let, const, and var?
let and const were introduced in ES6 (ECMAScript 2015) for declaring variables. let allows reassignment of the variable’s value, while const does not allow reassignment once a value is assigned. var is the pre-ES6 way of declaring variables and has function scope rather than block scope.
Explain the concept of AJAX.
AJAX (Asynchronous JavaScript and XML) is a technique used in web development for creating dynamic web pages that can update content without reloading the entire page. It involves making asynchronous requests to the server using JavaScript and handling the server’s response dynamically.
`
Explain closures in JavaScript.
Closures are inner functions that have access to the outer (enclosing) function’s variables. They “remember” the environment in which they were created, even after the outer function has finished executing. Closures are commonly used for data encapsulation and maintaining state in JavaScript.
What is hoisting in JavaScript?
Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their containing scope during compilation. This allows variables and functions to be used before they are declared. However, only the declarations are hoisted, not the initializations.
What is the event loop in JavaScript?
The event loop is the mechanism in JavaScript responsible for handling asynchronous operations. It continuously checks the call stack for any function that needs to be executed and the callback queue for any asynchronous tasks. It ensures that the call stack is never blocked, allowing non-blocking asynchronous behavior in JavaScript.
How can you handle errors in JavaScript?
Errors in JavaScript can be handled using try-catch blocks. Code that may throw an error is placed within the try block, and if an error occurs, it is caught in the catch block. This allows graceful error handling and prevents the script from crashing.
Explain the concept of callback functions.
Callback functions are functions that are passed as arguments to other functions and are executed after a specific task is completed. They are commonly used in asynchronous programming to handle asynchronous operations such as AJAX requests, event handling, and timeouts.
What is the DOM (Document Object Model)?
The DOM is a programming interface for web documents. It represents the structure of HTML documents as a tree-like structure, where each node represents an element. JavaScript can manipulate the DOM to dynamically change the content, structure, and style of web pages.
What is the purpose of the setTimeout function?
The setTimeout function is used to execute a function after a specified delay (in milliseconds). It allows for asynchronous execution of code and can be used for tasks such as animations, delays, and scheduling.
How does the “EventEmitter” class work in Node.js?
The EventEmitter class in Node.js allows objects to emit named events and register listener functions that will be called when the event is emitted. It facilitates building asynchronous, event-driven applications in Node.js by providing a way to handle events in a non-blocking manner.
What is the DOM (Document Object Model)?
The Document Object Model (DOM) is a programming interface for web documents. It represents the structure of HTML or XML documents as a tree-like structure, where each node corresponds to a part of the document, such as elements, attributes, and text. The DOM provides a way for programs (usually JavaScript) to interact with and manipulate the content, structure, and style of web pages dynamically.
What is the purpose of the setTimeout function?
The setTimeout function in JavaScript is used to execute a specified function or piece of code after a specified delay (in milliseconds). It allows for the asynchronous execution of code, enabling tasks like animations, delays, and scheduling. setTimeout is commonly used in scenarios where a certain action needs to be performed after a specific period of time.
Explain the difference between null and undefined.
Null and undefined are both primitive values in JavaScript that represent the absence of a value, but they have different use cases.
Null is explicitly assigned by developers to represent the absence of any value or as a placeholder for an intentionally missing value.
Undefined typically represents a variable that has been declared but not yet assigned a value. It is also the default value of uninitialized variables and the return value of functions that do not explicitly return anything.
How does prototypal inheritance work in JavaScript?
Prototypal inheritance is a fundamental feature of JavaScript’s object-oriented programming paradigm. In JavaScript, objects can inherit properties and methods from other objects through their prototype chain. Each object has an internal reference to another object called its prototype. When a property or method is accessed on an object, JavaScript looks for it first on the object itself and then up the prototype chain until it finds a match.
Explain event delegation in JavaScript and its benefits.
Event delegation is a technique in JavaScript where a single event listener is attached to a parent element, rather than individual child elements. When an event occurs on a child element, it “bubbles up” to the parent element where the event listener is attached. This allows for dynamic handling of events on elements that are added or removed dynamically, reducing the number of event listeners and improving performance.
What is the purpose of the “bind” method in JavaScript?
The bind method in JavaScript is used to create a new function with a specified this value and, optionally, pre-defined arguments. It allows for explicit setting of the this context for a function, regardless of how it is called. Bind is commonly used to create new functions with a fixed this value, particularly in event handling, callback functions, and method chaining scenarios.