Web Development
4.7 Rating 68 Questions 30 mins read208 Readers
In JavaScript, event capturing and event bubbling is two different ways that events can propagate through the Document Object Model (DOM). Event capturing is the process by which an event travels from the top of the DOM down to the target element, while event bubbling is the process by which an event travels from the target element up to the top of the DOM.
Both event capturing and event bubbling allow you to attach multiple event listeners to a single element and control the order in which they are executed. This can be useful for creating complex event-driven applications.
Here is an example of how event capturing and event bubbling work:
<div id="outer"> <div id="inner"><button>Click me</button></div> </div>
In this example, the button is the target element. If the button is clicked, the event will first be captured by the inner element, then by the outer element.
If we attach an event listener to the outer element using the addEventListener method and set the useCapture option to true, the event will be captured by the outer element first and then trickle down to the inner element.
This is an example of event capturing.
document.getElementById('outer').addEventListener('click', function(event) {
console.log('Outer element clicked');
}, true); This is one of the most frequently asked Front-end interview questions.
JavaScript allows us to check the empty state of an object by using the Object.keys() method, which returns an array of the object's own enumerable properties. If the object has no properties, the Object.keys() method will return an empty array.
We can use the length property of the array returned by Object.keys() to check if the object is empty or not. If the length of the array is 0, the object is empty.
Here is an example of how to check if an object is empty in JavaScript:
function isObjectEmpty(obj) { return Object.keys(obj).length === 0; } var obj = {}; console.log(isObjectEmpty(obj)); // Output: true obj = { a: 10, b: 12 }; console.log(isObjectEmpty(obj)); // Output: false
console.log(2 + true); console.log(true + ‘2’);
In JavaScript, boolean values are treated as numbers when used in arithmetic operations. true is treated as the number 1, and false is treated as the number 0. Therefore, the + operator performs addition and returns the sum of 2 and 1, which is 3.
In this case, the operands are the boolean value true and the string "2". Since one of the operands is a string, the + operator concatenates them, resulting in the string "true2".
A callback function in JavaScript is a function that is passed as an argument to another function and is executed after the latter function has completed its execution. Callback functions are commonly used in JavaScript and other programming languages to perform certain actions after an event has occurred or to execute a function asynchronously.
For example, a callback function may be passed as an argument to an event listener function, which will execute the callback function when the specified event occurs. Callback functions are often used to perform tasks that may take a long time to complete, such as making an HTTP request or reading from a file without blocking the main execution thread.
Expect to come across this, one of the most popular UI developer interview questions.
CSS selectors are used to select elements on an HTML page and apply styles to them. There are various types of CSS selectors, including element selectors, class selectors, and ID selectors, which can be used to select elements based on their tag name, class name, or ID, respectively.
Some examples of CSS selectors:
The factorial function uses a recursive approach to calculate the factorial of a given number. It returns n * factorial(n - 1) until it reaches the base case, at which point it returns 1.
function factorial(n) {
if (n === 0) {
return 1;
}
return n * factorial(n - 1);
}
console.log(factorial(5)); // outputs 120
Below are some intermediate level interview questions that will help you prepare well.
When we clone an object in JavaScript, we can either create a deep copy or a shallow copy.
Shallow Copy: A shallow copy of the referenced object is created when a reference variable is copied into a new reference variable using the assignment operator. An old reference variable's value is assigned to a new reference variable by copying its address. As a result, both the old and new reference variables refer to the same object in memory. Therefore, if any one of the reference variables changes, it will affect both.
// Code Snippet for Shallow Copy var employee = { ename: "abc", salary: 50000 } console.log(employee); // {ename: “abc”, salary: “5000”} var newEmployee = employee; // Shallow copy console.log(newEmployee); // {ename: “abc”, salary: “5000”} newEmployee.ename = "xyz"; console.log(employee); // {ename: “xyz”, salary: “5000”} console.log(newEmployee); // {ename: “xyz”, salary: “5000”} // ename of employee and newEmployee both are changed.
Deep Copy: In contrast to shallow copies, deep copies make copies of all the members of an object, allocate separate memory locations for the new object, and then assign the copied members to it. Hence, both the objects are independent from one another and any changes to either one will not affect the other. JSON.parse() and JSON.stringify() allow us to deep copy objects in JavaScript.
When the object has serializable properties and is small, this method is useful. However, if the object is very large and contains certain non-serializable properties, data loss may occur. So, CloneDeep() Method from Lodash library is the better way to deep clone the object.
A common occurrence in Front-end developer questions, don't miss this one.
One of the most frequently posed junior Front-end developer interview questions, be ready for it.
Arrow functions are a shorthand syntax for defining functions in JavaScript. They are similar to regular functions but have a shorter syntax and some differences in behaviour.
One of the main benefits of using arrow functions is that they can help reduce the amount of code we need to write.
For example, we can define a function with a single statement and no function keyword or curly braces:
const add = (a, b) => a + b;
This is equivalent to:
const add = function(a, b) {
return a + b;
}; Arrow functions also have a lexical this value, which means that this value inside the function is the same as the value outside the function. This can be useful when we need to access this inside a callback function, For example;
const obj = {
message: 'Hello',
sayHello: () => {
console.log(this.message);
}
};
obj.sayHello(); // logs "Hello" This is different from regular functions, where the value of this is determined by how the function is called.
A pure function is a function that has the following properties:
Pure functions have several benefits:
It is generally a good idea to write pure functions whenever possible, as they can help to make your code more predictable, maintainable, and efficient.
A staple in Front-end interview questions and answers, be prepared to answer this one.
A closure is a function that has access to the variables in its lexical scope, even when it is executed outside of that scope. Closures are created every time a function is defined inside another function. The inner function has access to the variables and parameters of the outer function, even after the outer function has returned.
Example:
function outerFunction(x) {
let y = x;
function innerFunction() {
console.log(y)
}
return innerFunction;
}
const myClosure = outerFunction(5);
myClosure(); // logs 5 In this example, the innerFunction has access to the y variable, even though it is defined and executed outside the outerFunction. This is because innerFunction is a closure that has access to the variables in its lexical scope.
Closures are often used to create private variables and functions that can only be accessed from within a specific scope. They can also be used to keep track of the state and create callback functions that have access to that state.
Overall, closures are an important and powerful concept in JavaScript, and they are often used to create more modular and maintainable code.
In HTML, a webpage can be displayed in either standards mode or quirks mode. These modes determine how a webpage is rendered by a web browser, and they can affect the way the webpage looks and behaves.
Standards mode is the preferred mode for rendering webpages, as it follows the standards set by the World Wide Web Consortium (W3C) for HTML, CSS, and other web technologies. When a webpage is displayed in standards mode, the web browser follows the standards specified in the HTML and CSS code, which ensures that the webpage is displayed consistently across different browsers.
Quirks mode, on the other hand, is a way for web browsers to handle older or improperly coded webpages. When a webpage is displayed in quirks mode, the web browser uses a different set of rules to try to display the webpage correctly. These rules are based on the way web browsers used to handle webpages in the past, and they can cause the webpage to be displayed differently in different browsers.
To determine which mode to use, web browsers look at the HTML code of the webpage and check for certain tags or attributes that indicate which mode to use.
For example, if the webpage includes a <!DOCTYPE> declaration at the top of the HTML code, the browser will use standards mode. If the <!DOCTYPE> declaration is missing or incorrect, the browser will use quirks mode.
Webpack is a module bundler for modern JavaScript applications. It is a powerful tool used by developers to write and organize their code in a modular manner, and then bundle it together into a single file (or a few files) that a web browser can load.
One of the main benefits of using webpack is that it allows developers to use the latest JavaScript features and syntax, even if the user's web browser does not support those features. Webpack also offers a range of other features and capabilities, such as:
There are several ways to reduce the page load time of a website or web application. Here are a few suggestions:
By implementing some or all of these strategies, one can significantly improve the page load time of your website or web application.
This is one of the frequently asked senior Front-end developer interview questions.
Dependency injection (DI) is a design pattern that allows an object to receive its dependencies from an external source rather than creating them itself. This can make it easier to design, test, and maintain complex systems, as it allows different components of the system to be isolated from one another and more easily modified or replaced.
In a DI system, objects are typically defined with interfaces that specify their dependencies. An external source, such as a container or factory, is responsible for creating and injecting the required dependencies into the object when it is needed. This means that the object does not need to know how to create its dependencies, or even what they are.
For example, consider a simple object that represents a car. The car object might have a dependency on an engine object, which it needs in order to operate. Without DI, the car object might create its own engine object using the new operator, like this:
class Car {
constructor() {
this.engine = new Engine();
}
} With DI, the car object would instead define an interface for its engine dependency, and the DI container would be responsible for creating and injecting the engine object when the car object is created:
class Car {
constructor(engine) {
this.engine = engine;
}
} Using DI has several benefits:
Overall, dependency injection is a useful design pattern that can help to improve the modularity, testability, and maintainability of complex systems.
Lazy loading is a technique that allows content to be loaded on demand, rather than all at once. This can help to improve the performance and load time of a webpage or web application, especially if it has a lot of content.
Lazy loading can be implemented in various ways, such as: