Interview Questions
Interview Questions
Service workers are used to intercept network requests. In a way, the service worker is like a proxy server
assisting in improving offline user experience and designing PWAs.
The intended use case for web workers is parallelism, while service workers are used for creating offline
support for a web app.
function display() {
var a = b = 10;
}
display();
----
b false
a true
This is because the assignment operator has right to left associativity in Javascript which means it will be
evaluated from right to left.
function display() {
var a = b = 10;
}
is same as
function display() {
var a = (b = 10);
}
which is same as
function display() {
b = 10;
var a = b;
}
So b becomes a global variable because there is no var keyword before it and a becomes a local variable.
Therefore, outside the function, only b is available so 𝘁𝘆𝗽𝗲𝗼𝗳 𝗮 === '𝘂𝗻𝗱𝗲𝗳𝗶𝗻𝗲𝗱' comes as 𝘁𝗿𝘂𝗲 and
𝘁𝘆𝗽𝗲𝗼𝗳 𝗯 === '𝘂𝗻𝗱𝗲𝗳𝗶𝗻𝗲𝗱' comes as 𝗳𝗮𝗹𝘀𝗲.
JavaScript does not have the concept of private constructors, so the most common way to achieve the
same effect is to use closures and immediately-invoked function expressions (IIFE)
In this example, the Person constructor is defined within an IIFE, which creates a closure around the
constructor and makes it private to the IIFE.
Palindrome strings
A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward
or forward. This includes capital letters, punctuation, and word dividers.
Implement a function that checks if something is a palindrome. If the input is a number, convert it to string
first.
Example 02:
Fibonacci Series
In mathematics, the Fibonacci sequence is a sequence in which each number is the sum of the two
preceding ones. Numbers that are part of the Fibonacci sequence are known as Fibonacci numbers.
displayFabonacciSeries(5); // 1 2 3 5 8
Factorial Number
In mathematics, the factorial of a non-negative integer n, denoted by n! is the product of all positive integers
less than or equal to n.
let result = 1;
for (let i = 1; i <= inputNumber; i++) {
result *= i;
};
console.log(result);
};
displayFactorial(5); // 120
Event Loop
The event loop is a mechanism that allows JavaScript to handle asynchronous operations by managing the
execution of code:
logA();
setTimeout(logB, 0);
Promise.resolve().then(logC);
logD();
Output: ?
if (!Array.isArray(arr)) {
throw new Error("This is not a valid array");
}
const reversedArray = [];
for (let i = 0; i < arr.length; i++) {
reversedArray[i] = arr[arr.length - 1 - i];
}
return reversedArray;
}
Example 2: