๐ฆธ๐ปโโ๏ธ Javascript Questions
๐ธ Are semicolons required in JavaScript?
new
keyword
๐ธ What happens when you call a constructor function with the null
vs undefined
๐ธ ๐ธ What is a closure, and how/why would you use this?
A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer functionโs scope from an inner function.
In JavaScript, closures are created every time a function is created, at function creation time.
Whenever a function is declared in JavaScript a closure is created. inside the IIFE
Closure Scope Chain: Every closure has three
scopes
- Local Scope (Own scope)
- Outer Functions Scope
- Global Scope
I came across great post on closure please refer this to understand more link
๐ธ Can you give a useful example of closure?
- closure can be used for function currying
- Situations where you might want to do this are particularly common on the web. Much of the code written in front-end JavaScript is event-based. You define some behavior, and then attach it to an event that is triggered by the user (such as a click or a keypress). The code is attached as a callback (a single function that is executed in response to the event).
- You can emulate private methods with closures. Languages such as Java allow you to declare methods as private but JavaScript does not provide a native way of doing this, but it is possible to emulate private methods using closures, this resulting the modularization
๐ธ What are the consideration that we need to avoid while working with closure
this
works
๐ธ Explain how ๐ธ Explain how prototypal inheritance works.
๐ธ How does prototypal inheritance differ from classical inheritance?
๐ธ What's a typical use case for anonymous functions?
๐ธ What's the difference between host objects and native objects?
curry function
and why this syntax offers an advantage?
๐ธ Can you give an example of a Event Loop
works and explain the individual component that take a part in it
๐ธ Explain how Promise
, Promise Chain
and its methods
๐ธ Explain ๐ธ Promise.race vs Promise.all
๐ธ How Callback function different then Promise and what problem promise can solve
async
and await
works and how it solved the problem of promise
๐ธ How ๐ธ What is Computed properties what us the typical use cases of this
๐ธ What is generators and how is it different from function
๐ธ What are the real use-case of the generators
Generators can be implement in replacement for an iterators
Let me show you with a simple example.
Here i am just trying to generate natural no from 1, 10 using an
Symbol.Iterator
let range = {from: 1,to: 10,[Symbol.iterator]() {return {current: this.from,last: this.to,next() {if (this.current < this.last) {return { done: false, value: this.current++ };} else {return { done: true };}},};},};console.log([...range]); // [1,2,3,4,5,6,7,8,9]Now the above can be simplified using generator
let range = {from: 1,to: 10,*[Symbol.iterator]() {for (let i = this.from; i < this.to; i++) {yield i;}},};console.log([...range]);Better Async functionality
Code using promises and callbacks such as
function fetchJson(url) {return fetch(url).then((request) => request.text()).then((text) => {return JSON.parse(text);}).catch((error) => {console.log(`ERROR: ${error.stack}`);});}can be written as (with the help of libraries such as
co.js
) which uses the generatorconst fetchJson = co.wrap(function* (url) {try {let request = yield fetch(url);let text = yield request.text();return JSON.parse(text);} catch (error) {console.log(`ERROR: ${error.stack}`);}});There are more applications of generators like
Lazy Evaluation
Memory efficiency
Data streaming
symbol
works explain its use-case
๐ธ How ๐ธ Explain Function Decomposition
๐ธ What is the difference between a parameter and an argument?
๐ธ Does JavaScript pass by value or by reference?
IIFE
and what are the use case of this?
๐ธ What is ๐ธ What is the reason for wrapping the entire contents of a JavaScript source file in a function that is immediately invoked?
๐ธ Can you offer a use case for the new arrow => function syntax? How does this new syntax differ from other functions?
Decorators
in javascript and When its suitable to use decorators
๐ธ What is bind()
๐ธ Write a polyfill for A bind()
using ES6 looks something like this
Now the Polyfill for the bind will look something like this
Here the highlighted code is a polyfill for bind
which does same things as native bind
๐ธ What is polyfill why is that required
๐ธ What is Transpiling in JS
๐ธ Explain Debounce and its applications
The debounce
function forces a function to wait a certain amount of time before running again. The function is built to limit the number of times a function is called.
simplified version of debounce
Applications of Debouncing
- Debouncing can be use to limit the no of API calls in a website
- It also used to keep the track of user scroll event (Infinity Scroll)
- Its helpful for to keep track of the window resize
๐ธ Explain Throttling and its applications
Throttling enforces a maximum number of times a function can be called over time. For example, โexecute this function at most once every 100 milliseconds.โ
Applications of Throttling
- Throttling can be used the action based polling system
- It can also fit in when there is use cases to hit a button multiple time