Fundamental Concepts
-
What is JavaScript, and how does it differ from Java?
-
JavaScript is a high-level, interpreted programming language used primarily for client-side web development, but it can also be used for server-side development with Node.js. It's dynamic, flexible, and known for its event-driven programming model.
-
Java is a general-purpose, object-oriented programming language that is compiled into bytecode and can run on any platform with a Java Virtual Machine (JVM). It's known for its strong typing, platform independence, and enterprise applications.
-
-
Explain the difference between var, let, and const.
-
var: Function-scoped, can be re-declared and re-assigned within the same scope.
-
let: Block-scoped, cannot be re-declared but can be re-assigned within the same scope.
-
const: Block-scoped, cannot be re-declared or re-assigned once initialized.
-
-
What is hoisting in JavaScript?
-
Hoisting is a JavaScript mechanism that moves variable declarations to the top of their scope, regardless of where they are actually placed. Function declarations are also hoisted to the top.
-
-
Explain closures in JavaScript.
-
A closure is a function that has access to variables in its outer (enclosing) function, even after the outer function has returned. This is possible because closures create a new scope that retains references to variables from the outer scope.
-
-
What is the difference between null and undefined?
-
null: A value that represents the absence of an object.
-
undefined: A value that indicates a variable has not been assigned a value yet.
-
Object-Oriented Programming
-
Explain prototype inheritance in JavaScript.
-
JavaScript uses prototype-based inheritance, where objects inherit properties and methods from their prototype object. A prototype chain is formed, allowing objects to access properties and methods from their parent prototypes.
-
-
What is the difference between
thiskeyword in different contexts?-
Function context:
thisrefers to the object that the function is called on. -
Object method context:
thisrefers to the object itself. -
Constructor context:
thisrefers to the new object being created. -
Global context:
thisrefers to the global object (e.g.,windowin a browser).
-
-
How do you create an object in JavaScript?
-
Object literal:
let person = { name: "Alice", age: 30 }; -
Constructor function:
function Person(name, age) { this.name = name; this.age = age; } -
Class syntax:
class Person { constructor(name, age) { this.name = name; this.age = age; } }
-
DOM Manipulation
-
How do you select elements in the DOM using JavaScript?
-
getElementById: Selects an element by its ID.
-
getElementsByTagName: Selects elements by their tag name.
-
getElementsByClassName: Selects elements by their class name.
-
querySelector: Selects the first element that matches a CSS selector.
-
querySelectorAll: Selects all elements that match a CSS selector.
-
-
What is the difference between innerHTML and textContent?
-
innerHTML: Sets or gets the HTML content of an element, including child elements and their attributes.
-
textContent: Sets or gets the plain text content of an element, excluding child elements and their attributes.
Events and Asynchronous Programming
-
Explain event bubbling and event capturing.
-
Event bubbling: Events propagate up the DOM hierarchy from the target element to its ancestor elements.
-
Event capturing: Events propagate down the DOM hierarchy from the outermost element to the target element.
-
What is the purpose of the
addEventListenermethod?
-
The
addEventListenermethod attaches an event handler to an element, allowing you to specify the event type and the function to be called when the event occurs.
-
How do you handle asynchronous operations in JavaScript?
-
Callbacks: Functions that are passed as arguments to other functions and are executed when the asynchronous operation completes.
-
Promises: Objects that represent the eventual completion (or failure) of an asynchronous operation.
-
async/await: Syntax that makes asynchronous code look more like synchronous code.
Advanced Topics
-
What is the difference between synchronous and asynchronous code execution?
-
Synchronous code: Executes line by line, blocking the execution of subsequent code until the current line is finished.
-
Asynchronous code: Executes non-blocking operations, allowing the program to continue executing while waiting for the asynchronous operation to complete.
-
Explain the concept of a module in JavaScript.
-
A module is a reusable unit of code that can be imported into other modules. JavaScript modules can be created using CommonJS, ES modules, or AMD.
-
What is the purpose of the
strict modein JavaScript?
-
Strict mode is a mode that enforces stricter rules for JavaScript code, such as preventing certain errors and enforcing best practices.
-
What is the difference between a regular expression and a string?
-
Regular expression: A pattern that matches a set of characters.
-
String: A sequence of characters.
-
Explain the concept of a scope chain in JavaScript.
-
A scope chain is the hierarchy of scopes that are accessible to a given piece of code. When a variable is referenced, the JavaScript engine searches the scope chain from the innermost scope to the outermost scope until it finds the variable.
-
What is the purpose of the
typeofoperator?
-
The
typeofoperator returns a string indicating the type of the operand.
-
How do you create a custom event in JavaScript?
-
You can create a custom event using the
CustomEventconstructor.
Performance and Optimization
-
How can you improve the performance of your JavaScript code?
-
Minimize DOM manipulations: Avoid frequent updates to the DOM, as they can be expensive.
-
Use event delegation: Attach event handlers to a parent element and use event bubbling or capturing to handle events for child elements.
-
Optimize JavaScript code: Minimize the number of calculations and avoid unnecessary operations.
-
Use efficient data structures: Choose data structures that are appropriate for the task at hand.
-
What is the difference between
==and===operators?
-
==: Performs type coercion before comparison.
-
===: Compares both value and type.
-
Explain the concept of garbage collection in JavaScript.
-
Garbage collection is the process of automatically freeing memory that is no longer being used. JavaScript engines typically use a mark-and-sweep algorithm for garbage collection.
-
What is the purpose of the
deferandasyncattributes for<script>tags?
-
defer: The script will be executed after the page has finished parsing, but before the DOMContentLoaded event fires.
-
async: The script will be executed as soon as it is available, without waiting for the rest of the page to be parsed.
-
What is the difference between a function expression and a function declaration?
-
Function expression: A function that is assigned to a variable.
-
Function declaration: A function that is declared using the
functionkeyword.