| No. | Questions |
|---|
| 351 | What is a thunk function | | 352 | What are asynchronous thunks | | 353 | What is the output of below function calls | | 354 | How to remove all line breaks from a string | | 355 | What is the difference between reflow and repaint | | 356 | What happens with negating an array | | 357 | What happens if we add two arrays | | 358 | What is the output of prepend additive operator on falsy values | | 359 | How do you create self string using special characters | | 360 | How do you remove falsy values from an array | | 361 | How do you get unique values of an array | | 362 | What is destructuring aliases | | 363 | How do you map the array values without using map method | | 364 | How do you empty an array | | 365 | How do you rounding numbers to certain decimals | | 366 | What is the easiest way to convert an array to an object | | 367 | How do you create an array with some data | | 368 | What are the placeholders from console object | | 369 | Is it possible to add CSS to console messages | | 370 | What is the purpose of dir method of console object | | 371 | Is it possible to debug HTML elements in console | | 372 | How do you display data in a tabular format using console object | | 373 | How do you verify that an argument is a Number or not | | 374 | How do you create copy to clipboard button | | 375 | What is the shortcut to get timestamp | | 376 | How do you flattening multi dimensional arrays | | 377 | What is the easiest multi condition checking | | 378 | How do you capture browser back button | | 379 | How do you disable right click in the web page | | 380 | What are wrapper objects | | 381 | What is AJAX | | 382 | What are the different ways to deal with Asynchronous Code | | 383 | How to cancel a fetch request | | 384 | What is web speech API | | 385 | What is minimum timeout throttling | | 386 | How do you implement zero timeout in modern browsers | | 387 | What are tasks in event loop | | 388 | What is microtask | | 389 | What are different event loops | | 390 | What is the purpose of queueMicrotask | | 391 | How do you use javascript libraries in typescript file | | 392 | What are the differences between promises and observables | | 393 | What is heap | | 394 | What is an event table | | 395 | What is a microTask queue | | 396 | What is the difference between shim and polyfill | | 397 | How do you detect primitive or non primitive value type | | 398 | What is babel | | 399 | Is Node.js completely single threaded | | 400 | What are the common use cases of observables |
-
A thunk is just a function which delays the evaluation of the value. It doesn’t take any arguments but gives the value whenever you invoke the thunk. i.e, It is used not to execute now but it will be sometime in the future. Let's take a synchronous example,
const add = (x, y) => x + y; const thunk = () => add(2, 3); thunk(); // 5
-
The asynchronous thunks are useful to make network requests. Let's see an example of network requests,
function fetchData(fn) { fetch("https://jsonplaceholder.typicode.com/todos/1") .then((response) => response.json()) .then((json) => fn(json)); } const asyncThunk = function () { return fetchData(function getData(data) { console.log(data); }); }; asyncThunk();
The
getDatafunction won't be called immediately but it will be invoked only when the data is available from API endpoint. The setTimeout function is also used to make our code asynchronous. The best real time example is redux state management library which uses the asynchronous thunks to delay the actions to dispatch. -
Code snippet:
const circle = { radius: 20, diameter() { return this.radius * 2; }, perimeter: () => 2 * Math.PI * this.radius, };
console.log(circle.diameter()); console.log(circle.perimeter());
Output:
The output is 40 and NaN. Remember that diameter is a regular function, whereas the value of perimeter is an arrow function. The
thiskeyword of a regular function(i.e, diameter) refers to the surrounding scope which is a class(i.e, Shape object). Whereas this keyword of perimeter function refers to the surrounding scope which is a window object. Since there is no radius property on window objects it returns an undefined value and the multiple of number value returns NaN value. -
The easiest approach is using regular expressions to detect and replace newlines in the string. In this case, we use replace function along with string to replace with, which in our case is an empty string.
function remove_linebreaks( var message ) { return message.replace( /[\r\n]+/gm, "" ); }
In the above expression, g and m are for global and multiline flags.
-
A repaint occurs when changes are made which affect the visibility of an element, but not its layout. Examples of this include outline, visibility, or background color. A reflow involves changes that affect the layout of a portion of the page (or the whole page). Resizing the browser window, changing the font, content changing (such as user typing text), using JavaScript methods involving computed styles, adding or removing elements from the DOM, and changing an element's classes are a few of the things that can trigger reflow. Reflow of an element causes the subsequent reflow of all child and ancestor elements as well as any elements following it in the DOM.
-
Negating an array with
!character will coerce the array into a boolean. Since Arrays are considered to be truthy So negating it will returnfalse.console.log(![]); // false
-
If you add two arrays together, it will convert them both to strings and concatenate them. For example, the result of adding arrays would be as below,
console.log(["a"] + ["b"]); // "ab" console.log([] + []); // "" console.log(![] + []); // "false", because ![] returns false.
-
If you prepend the additive(+) operator on falsy values(null, undefined, NaN, false, ""), the falsy value converts to a number value zero. Let's display them on browser console as below,
console.log(+null); // 0 console.log(+undefined); // NaN console.log(+false); // 0 console.log(+NaN); // NaN console.log(+""); // 0
-
The self string can be formed with the combination of
[]()!+characters. You need to remember the below conventions to achieve this pattern.- Since Arrays are truthful values, negating the arrays will produce false: ![] === false
- As per JavaScript coercion rules, the addition of arrays together will toString them: [] + [] === ""
- Prepend an array with + operator will convert an array to false, the negation will make it true and finally converting the result will produce value '1': +(!(+[])) === 1
By applying the above rules, we can derive below conditions
(![] + [] === "false" + !+[]) === 1;
Now the character pattern would be created as below,
s e l f ^^^^^^^^^^^^^ ^^^^^^^^^^^^^ ^^^^^^^^^^^^^ ^^^^^^^^^^^^^ (![] + [])[3] + (![] + [])[4] + (![] + [])[2] + (![] + [])[0] ^^^^^^^^^^^^^ ^^^^^^^^^^^^^ ^^^^^^^^^^^^^ ^^^^^^^^^^^^^ (![] + [])[+!+[]+!+[]+!+[]] + (![] + [])[+!+[]+!+[]+!+[]+!+[]] + (![] + [])[+!+[]+!+[]] + (![] + [])[+[]] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ (![]+[])[+!+[]+!+[]+!+[]]+(![]+[])[+!+[]+!+[]+!+[]+!+[]]+(![]+[])[+!+[]+!+[]]+(![]+[])[+[]]
-
You can apply the filter method on the array by passing Boolean as a parameter. This way it removes all falsy values(0, undefined, null, false and "") from the array.
const myArray = [false, null, 1, 5, undefined]; myArray.filter(Boolean); // [1, 5] // is same as myArray.filter(x => x);
-
You can get unique values of an array with the combination of
Setand rest expression/spread(...) syntax.console.log([...new Set([1, 2, 4, 4, 3])]); // [1, 2, 4, 3]
-
Sometimes you would like to have a destructured variable with a different name than the property name. In that case, you'll use a
: newNameto specify a name for the variable. This process is called destructuring aliases.const obj = { x: 1 }; // Grabs obj.x as as { otherName } const { x: otherName } = obj;
-
You can map the array values without using the
mapmethod by just using thefrommethod of Array. Let's map city names from Countries array,const countries = [ { name: "India", capital: "Delhi" }, { name: "US", capital: "Washington" }, { name: "Russia", capital: "Moscow" }, { name: "Singapore", capital: "Singapore" }, { name: "China", capital: "Beijing" }, { name: "France", capital: "Paris" }, ]; const cityNames = Array.from(countries, ({ capital }) => capital); console.log(cityNames); // ['Delhi, 'Washington', 'Moscow', 'Singapore', 'Beijing', 'Paris']
-
You can empty an array quickly by setting the array length to zero.
let cities = ["Singapore", "Delhi", "London"]; cities.length = 0; // cities becomes []
-
You can round numbers to a certain number of decimals using
toFixedmethod from native javascript.let pie = 3.141592653; pie = pie.toFixed(3); // 3.142
-
You can convert an array to an object with the same data using spread(...) operator.
var fruits = ["banana", "apple", "orange", "watermelon"]; var fruitsObject = { ...fruits }; console.log(fruitsObject); // {0: "banana", 1: "apple", 2: "orange", 3: "watermelon"}
-
You can create an array with some data or an array with the same values using
fillmethod.var newArray = new Array(5).fill("0"); console.log(newArray); // ["0", "0", "0", "0", "0"]
-
Below are the list of placeholders available from console object,
- %o — It takes an object,
- %s — It takes a string,
- %d — It is used for a decimal or integer These placeholders can be represented in the console.log as below
const user = { name: "John", id: 1, city: "Delhi" }; console.log( "Hello %s, your details %o are available in the object form", "John", user ); // Hello John, your details {name: "John", id: 1, city: "Delhi"} are available in object
-
Yes, you can apply CSS styles to console messages similar to html text on the web page.
console.log( "%c The text has blue color, with large font and red background", "color: blue; font-size: x-large; background: red" );
The text will be displayed as below,

Note: All CSS styles can be applied to console messages.
-
The
console.dir()is used to display an interactive list of the properties of the specified JavaScript object as JSON.const user = { name: "John", id: 1, city: "Delhi" }; console.dir(user);
-
Yes, it is possible to get and debug HTML elements in the console just like inspecting elements.
const element = document.getElementsByTagName("body")[0]; console.log(element);
It prints the HTML element in the console,
-
The
console.table()is used to display data in the console in a tabular format to visualize complex arrays or objects.const users = [ { name: "John", id: 1, city: "Delhi" }, { name: "Max", id: 2, city: "London" }, { name: "Rod", id: 3, city: "Paris" }, ]; console.table(users);
The data visualized in a table format,
-
The combination of IsNaN and isFinite methods are used to confirm whether an argument is a number or not.
function isNumber(n) { return !isNaN(parseFloat(n)) && isFinite(n); }
-
You need to select the content(using .select() method) of the input element and execute the copy command with execCommand (i.e, execCommand('copy')). You can also execute other system commands like cut and paste.
document.querySelector("#copy-button").onclick = function () { // Select the content document.querySelector("#copy-input").select(); // Copy to the clipboard document.execCommand("copy"); };
-
You can use
new Date().getTime()to get the current timestamp. There is an alternative shortcut to get the value.console.log(+new Date()); console.log(Date.now());
-
Flattening bi-dimensional arrays is trivial with Spread operator.
const biDimensionalArr = [11, [22, 33], [44, 55], [66, 77], 88, 99]; const flattenArr = [].concat(...biDimensionalArr); // [11, 22, 33, 44, 55, 66, 77, 88, 99]
But you can make it work with multi-dimensional arrays by recursive calls,
function flattenMultiArray(arr) { const flattened = [].concat(...arr); return flattened.some((item) => Array.isArray(item)) ? flattenMultiArray(flattened) : flattened; } const multiDimensionalArr = [11, [22, 33], [44, [55, 66, [77, [88]], 99]]]; const flatArr = flattenMultiArray(multiDimensionalArr); // [11, 22, 33, 44, 55, 66, 77, 88, 99]
Also you can use the
flatmethod of Array.const arr = [1, [2,3], 4, 5, [6,7]]; const fllattenArr = arr.flat(); // [1, 2, 3, 4, 5, 6, 7] // And for multiDemensional arrays const multiDimensionalArr = [11, [22, 33], [44, [55, 66, [77, [88]], 99]]]; const oneStepFlat = multiDimensionalArr.flat(1); // [11, 22, 33, 44, [55, 66, [77, [88]], 99]] const towStep = multiDimensionalArr.flat(2); // [11, 22, 33, 44, 55, 66, [77, [88]], 99] const fullyFlatArray = multiDimensionalArr.flat(Infinity); // [11, 22, 33, 44, 55, 66, 77, 88, 99]
-
You can use
indexOfto compare input with multiple values instead of checking each value as one condition.// Verbose approach if ( input === "first" || input === 1 || input === "second" || input === 2 ) { someFunction(); } // Shortcut if (["first", 1, "second", 2].indexOf(input) !== -1) { someFunction(); }
-
The
window.onbeforeunloadmethod is used to capture browser back button events. This is helpful to warn users about losing the current data.window.onbeforeunload = function () { alert("You work will be lost"); };
-
The right click on the page can be disabled by returning false from the
oncontextmenuattribute on the body element.<body oncontextmenu="return false;"></body>
-
Primitive Values like string,number and boolean don't have properties and methods but they are temporarily converted or coerced to an object(Wrapper object) when you try to perform actions on them. For example, if you apply toUpperCase() method on a primitive string value, it does not throw an error but returns uppercase of the string.
let name = "john"; console.log(name.toUpperCase()); // Behind the scenes treated as console.log(new String(name).toUpperCase());
i.e, Every primitive except null and undefined have Wrapper Objects and the list of wrapper objects are String,Number,Boolean,Symbol and BigInt.
-
AJAX stands for Asynchronous JavaScript and XML and it is a group of related technologies(HTML, CSS, JavaScript, XMLHttpRequest API etc) used to display data asynchronously. i.e. We can send data to the server and get data from the server without reloading the web page.
-
Below are the list of different ways to deal with Asynchronous code.
- Callbacks
- Promises
- Async/await
- Third-party libraries such as async.js,bluebird etc
-
Until a few days back, One shortcoming of native promises is no direct way to cancel a fetch request. But the new
AbortControllerfrom js specification allows you to use a signal to abort one or multiple fetch calls. The basic flow of cancelling a fetch request would be as below,- Create an
AbortControllerinstance - Get the signal property of an instance and pass the signal as a fetch option for signal
- Call the AbortController's abort property to cancel all fetches that use that signal For example, let's pass the same signal to multiple fetch calls will cancel all requests with that signal,
const controller = new AbortController(); const { signal } = controller; fetch("http://localhost:8000", { signal }) .then((response) => { console.log(`Request 1 is complete!`); }) .catch((e) => { if (e.name === "AbortError") { // We know it's been canceled! } }); fetch("http://localhost:8000", { signal }) .then((response) => { console.log(`Request 2 is complete!`); }) .catch((e) => { if (e.name === "AbortError") { // We know it's been canceled! } }); // Wait 2 seconds to abort both requests setTimeout(() => controller.abort(), 2000);
- Create an
-
Web speech API is used to enable modern browsers recognize and synthesize speech(i.e, voice data into web apps). This API has been introduced by W3C Community in the year 2012. It has two main parts,
- SpeechRecognition (Asynchronous Speech Recognition or Speech-to-Text): It provides the ability to recognize voice context from an audio input and respond accordingly. This is accessed by the
SpeechRecognitioninterface. The below example shows on how to use this API to get text from speech,
window.SpeechRecognition = window.webkitSpeechRecognition || window.SpeechRecognition; // webkitSpeechRecognition for Chrome and SpeechRecognition for FF const recognition = new window.SpeechRecognition(); recognition.onresult = (event) => { // SpeechRecognitionEvent type const speechToText = event.results[0][0].transcript; console.log(speechToText); }; recognition.start();
In this API, browser is going to ask you for permission to use your microphone
- SpeechSynthesis (Text-to-Speech): It provides the ability to recognize voice context from an audio input and respond. This is accessed by the
SpeechSynthesisinterface. For example, the below code is used to get voice/speech from text,
if ("speechSynthesis" in window) { var speech = new SpeechSynthesisUtterance("Hello World!"); speech.lang = "en-US"; window.speechSynthesis.speak(speech); }
The above examples can be tested on chrome(33+) browser's developer console. Note: This API is still a working draft and only available in Chrome and Firefox browsers(ofcourse Chrome only implemented the specification)
- SpeechRecognition (Asynchronous Speech Recognition or Speech-to-Text): It provides the ability to recognize voice context from an audio input and respond accordingly. This is accessed by the
-
Both browser and NodeJS javascript environments throttles with a minimum delay that is greater than 0ms. That means even though setting a delay of 0ms will not happen instantaneously. Browsers: They have a minimum delay of 4ms. This throttle occurs when successive calls are triggered due to callback nesting(certain depth) or after a certain number of successive intervals. Note: The older browsers have a minimum delay of 10ms. Nodejs: They have a minimum delay of 1ms. This throttle happens when the delay is larger than 2147483647 or less than 1. The best example to explain this timeout throttling behavior is the order of below code snippet.
function runMeFirst() { console.log("My script is initialized"); } setTimeout(runMeFirst, 0); console.log("Script loaded");
and the output would be in
Script loaded My script is initialized
If you don't use
setTimeout, the order of logs will be sequential.function runMeFirst() { console.log("My script is initialized"); } runMeFirst(); console.log("Script loaded");
and the output is,
My script is initialized Script loaded
-
You can't use setTimeout(fn, 0) to execute the code immediately due to minimum delay of greater than 0ms. But you can use window.postMessage() to achieve this behavior.
-
A task is any javascript code/program which is scheduled to be run by the standard mechanisms such as initially starting to run a program, run an event callback, or an interval or timeout being fired. All these tasks are scheduled on a task queue. Below are the list of use cases to add tasks to the task queue,
- When a new javascript program is executed directly from console or running by the
<script>element, the task will be added to the task queue. - When an event fires, the event callback added to task queue
- When a setTimeout or setInterval is reached, the corresponding callback added to task queue
- When a new javascript program is executed directly from console or running by the
-
Microtask is the javascript code which needs to be executed immediately after the currently executing task/microtask is completed. They are kind of blocking in nature. i.e, The main thread will be blocked until the microtask queue is empty. The main sources of microtasks are Promise.resolve, Promise.reject, MutationObservers, IntersectionObservers etc
Note: All of these microtasks are processed in the same turn of the event loop. ⬆ Back to Top
-
In JavaScript, there are multiple event loops that can be used depending on the context of your application. The most common event loops are:
1. The Browser Event Loop
2. The Node.js Event Loop
- Browser Event Loop: The Browser Event Loop is used in client-side JavaScript applications and is responsible for handling events that occur within the browser environment, such as user interactions (clicks, keypresses, etc.), HTTP requests, and other asynchronous actions.
- The Node.js Event Loop is used in server-side JavaScript applications and is responsible for handling events that occur within the Node.js runtime environment, such as file I/O, network I/O, and other asynchronous actions.
**[⬆ Back to Top](#table-of-contents)**
-
The
queueMicrotaskfunction is used to schedule a microtask, which is a function that will be executed asynchronously in the microtask queue. The purpose ofqueueMicrotaskis to ensure that a function is executed after the current task has finished, but before the browser performs any rendering or handles user events.Example:
console.log('Start'); //1 queueMicrotask(() => { console.log('Inside microtask'); // 3 }); console.log('End'); //2
By using queueMicrotask, you can ensure that certain tasks or callbacks are executed at the earliest opportunity during the JavaScript event loop, making it useful for performing work that needs to be done asynchronously but with higher priority than regular
setTimeoutorsetIntervalcallbacks. -
It is known that not all JavaScript libraries or frameworks have TypeScript declaration files. But if you still want to use libraries or frameworks in our TypeScript files without getting compilation errors, the only solution is
declarekeyword along with a variable declaration. For example, let's imagine you have a library calledcustomLibrarythat doesn’t have a TypeScript declaration and have a namespace calledcustomLibraryin the global namespace. You can use this library in typescript code as below,declare var customLibrary;
In the runtime, typescript will provide the type to the
customLibraryvariable asanytype. The another alternative without using declare keyword is belowvar customLibrary: any;
-
Some of the major difference in a tabular form
Promises Observables Emits only a single value at a time Emits multiple values over a period of time(stream of values ranging from 0 to multiple) Eager in nature; they are going to be called immediately Lazy in nature; they require subscription to be invoked Promise is always asynchronous even though it resolved immediately Observable can be either synchronous or asynchronous Doesn't provide any operators Provides operators such as map, forEach, filter, reduce, retry, and retryWhen etc Cannot be canceled Canceled by using unsubscribe() method -
Heap(Or memory heap) is the memory location where objects are stored when we define variables. i.e, This is the place where all the memory allocations and de-allocation take place. Both heap and call-stack are two containers of JS runtime. Whenever runtime comes across variables and function declarations in the code it stores them in the Heap.
-
Event Table is a data structure that stores and keeps track of all the events which will be executed asynchronously like after some time interval or after the resolution of some API requests. i.e Whenever you call a setTimeout function or invoke async operation, it is added to the Event Table. It doesn't not execute functions on it’s own. The main purpose of the event table is to keep track of events and send them to the Event Queue as shown in the below diagram.
-
Microtask Queue is the new queue where all the tasks initiated by promise objects get processed before the callback queue. The microtasks queue are processed before the next rendering and painting jobs. But if these microtasks are running for a long time then it leads to visual degradation.
-
A shim is a library that brings a new API to an older environment, using only the means of that environment. It isn't necessarily restricted to a web application. For example, es5-shim.js is used to emulate ES5 features on older browsers (mainly pre IE9). Whereas polyfill is a piece of code (or plugin) that provides the technology that you, the developer, expect the browser to provide natively. In a simple sentence, A polyfill is a shim for a browser API.
-
In JavaScript, primitive types include boolean, string, number, BigInt, null, Symbol and undefined. Whereas non-primitive types include the Objects. But you can easily identify them with the below function,
var myPrimitive = 30; var myNonPrimitive = {}; function isPrimitive(val) { return Object(val) !== val; } isPrimitive(myPrimitive); isPrimitive(myNonPrimitive);
If the value is a primitive data type, the Object constructor creates a new wrapper object for the value. But If the value is a non-primitive data type (an object), the Object constructor will give the same object.
-
Babel is a JavaScript transpiler to convert ECMAScript 2015+ code into a backwards compatible version of JavaScript in current and older browsers or environments. Some of the main features are listed below,
- Transform syntax
- Polyfill features that are missing in your target environment (using @babel/polyfill)
- Source code transformations (or codemods)
-
Node is a single thread, but some of the functions included in the Node.js standard library(e.g, fs module functions) are not single threaded. i.e, Their logic runs outside of the Node.js single thread to improve the speed and performance of a program.
-
Some of the most common use cases of observables are web sockets with push notifications, user input changes, repeating intervals, etc




