I hope you have found this quick list of common JavaScript interview questions and answers useful. Thank you for reading! Show
Don’t build web monoliths. Use Bit to create and compose decoupled software components — in your favorite frameworks like React or Node. Build scalable and modular applications with a powerful and enjoyable dev experience. Bring your team to Bit Cloud to host and collaborate on components together, and speed up, scale, and standardize development as a team. Try composable frontends with a Design System or Micro Frontends, or explore the composable backend with serverside components. I've carefully gone through over 50 resources, I've been through 10 JavaScript interviews, and I've landed a job at a unicorn startup. And throughout this entire process, I started to see a pattern in the most frequently asked JS interview questions. In this article, I have tried to list the concepts which will cover 80% of any good JS interview. So, if you are prepping for your next JS interview this is the perfect cheatsheet for you to review and solidify your skills. Go through this and you'll be ready to rock. 💃 📝Prerequisites
Table Of Contents 📜
Caveat: The focus here will largely be to cover concepts relevant to the interview and not to create a comprehensive booklet for learning the language. Treat this more like a cheatsheet. If you want to dive in deeply and learn more JS concepts, check out freeCodeCamp's curriculum. With that out of the way - let's go! JavaScript Basics 👶Let's start off with some basic concepts every JS developer needs to know. Variables in JavaScript 📥Variables are the building blocks of every programming language. You use them to store values. A variable can be a number, string, and many other types. Now, JS is a loosely-typed language. You don't have to state the type of variable. You can just declare it, and JS will figure it out on its own. Now, in JavaScript we have 3 ways to declare variables: 9, 0, and 1.Here are the key differences: Let's try to understand them through examples. We will cover scope later on. For now, let's focus on the other differences.
Note: In JavaScript, putting a semi-colon after the end of statement is optional. I will be skipping it here for the sake of readability. == vs === in JavaScriptLet's compare some variables. There are two ways you can do that. 2 only checks for the value 3 checks for value + type
Arrays in JavaScriptNow that we know a bit about variables, let's move on to arrays and array-methods. If we have declared a lot of variables, it makes sense to store them somewhere. Otherwise it will be difficult to keep track of all of them. Arrays are one way of storing a variable.
But only storing variables in an array is kind of boring. We can do more stuff with this array (like accessing these variables or changing the order in which they are stored or how they are stored). For that, JS has a lot of methods. Let's look at some of them now. JavaScript Array Methods 🧰The most frequently used array methods in JS are: 4, 5, 6, 7, and 8.Let's cover 4, 5, and 8. You can explore more in this helpful article.The // Return the words with more than 6 letters const words = ['react', 'script', 'interview', 'style', 'javascript'] const ans = words.filter((word) => word.length > 6) console.log(ans) // ['interview', 'javascript'] 4 array method 4 creates a new copy of the original array. We use it when we want to do something with the elements of the original array but don't want to change it. 4 iterates over the original array and takes a callback function (which we'll cover later) as an argument. In the callback function, we tell it what to do with the elements.
The // Return the words with more than 6 letters const words = ['react', 'script', 'interview', 'style', 'javascript'] const ans = words.filter((word) => word.length > 6) console.log(ans) // ['interview', 'javascript'] 5 array method 5 creates a new array with elements that meet the given condition(s).Let's look at an example. I have used arrow functions here. If you are a little uncomfortable with functions, you can cover the next section first and come back.
Try to do the exercises yourself first to test your knowledge. If you come up with different or better solutions, let me know! Generally, a follow up to this: can you do it without the array method?
The // Return the words with more than 6 letters const words = ['react', 'script', 'interview', 'style', 'javascript'] const ans = words.filter((word) => word.length > 6) console.log(ans) // ['interview', 'javascript'] 8 array method 8 is very similar to 4 but has two key differences:First of all, 4 returns a new Array, but 8 doesn't.
And second, you can do method chaining in 4 but not in 8.
Note: 4 and 8 don't mutate (change) the original array.Functional Programming in JavaScript 🛠We have already used functions above. Let's cover them in more detail now. Just like how we used variables to store values, we can use functions to store a piece of code which we can reuse. You can make function in two ways:
Now, let's cover some important concepts related to functions. Function Scope in JavaScript 🕵️Scope determines from where the variables are accessible. There are three types of scope:
Remember from before that 9 is globally scoped whereas 0 and 1 are block scoped. Let's understand that now. 0Closures in JavaScript (❗important) 🔒We have already used a closure without even realizing it. In the example below, 9 is a closed-over-variable. 1This section will have a lot of fancy words, so bear with me. We will cover them one by one. MDN says:
Okay, what is a lexical environment? It is essentially the surrounding state – the local memory along with the lexical environment of its parent. Whaaat? 🤯 I know it's a bit of a doozy. Let's understand it with a simple example. 2When x is invoked, y is returned. Now, y is waiting to be executed. Kind of like a loaded gun waiting to be shot! 🔫 So, when we finally invoke z, y is invoked. Now, y has to log 0 so it first tries to find 🔍 it in the local memory but it's not there. It goes to its parent function. It finds 0 there.Voila! There you have it - this is closure. Even when functions are returned (in the above case y) they still remember their lexical scope (where it came from) Totally unrelated quote for kicks 👻:
I swear the rest of the article is legit 🤞 Keep reading. Advantages of Closures in JavaScript 😎
3
Suppose you want to create a counter application. Every time you call it, the count increases by 1. But you don't want to expose the variable outside the function. How to do it? You guessed it – closures! 4Don't worry about 2 and 3. We have a whole section devoted to them down below.Disadvantages of Closures in JavaScript 😅
For example, the closed-over-variable will not be garbage collected. This is because, even if the outer function has run, the returned inner function still has a reference to the closed-over-variable. Note: Garbage collection basically removes unused variables from the memory automatically. Hoisting in JavaScript 🚩This is JavaScript's default behavior of moving declarations to the top of the program.
Let's look at an example: 5Phew! I am done with functions here, but if you want more check out this amazing talk by Anjana Vakil on functional programming. Objects in JavaScript 🔮Just like arrays, objects are a way of storing data. We do so with the help of key-value pairs. 6 9 is the 0 and 1 is the 2. Keys are generally the name of the properties of the object.We can store all sorts of data like functions inside an object. You can explore more here on the MDN. What is function consoleEven(arr) { let data = arr.forEach((num) => (num % 2 === 0 ? num * 2 : num * 1)) console.log(data) // undefined } consoleEven(arr) 2 in JavaScript?Now, working with objects is different in JS than in other popular programming languages like C++. And to understand that properly, we need a good grasp of the 2 keyword.Let's try to understand it step-by-step. In a program, at times, we need a way to point at stuff. Like saying this function right here belongs to this object. 2 helps us get this context.You will understand what I am saying better when we look at some examples. For now, think of 2 as something which provides context. And remember this important thing: its value depends on how and where it is called.I know, I know. A lot of 2 😬. Let's go over all this slowly.Start a new program and just log 2. 7It will point to the window object. Now, let's take an example with an object: 8Now, 2 will point to the object. So what's happening here?In the first example, we had nothing left of the 0 so it defaulted to the 1 object. But in this example, we have the object 2.If you do: 9We again get the 1 object. So, we can see that the value of 2 depends on how and where are we doing the calling.What we just did above is called Implicit Binding. The value of 2 got bound to the object.There is another way to use 2. Explicit binding is when you force a function to use a certain object as its 2.Let's understand why we need explicit binding through an example. 0We are using 2 properly, but can you see the problem with the above code?We are repeating code. And one of the principles of good programming is keep your code DRY! (Don't Repeat Yourself) So, let's get rid of 9 and simply do: 1 00 forced 01 to use the second object as its 2.There are a lot of other ways we can do this. Try to solve the given problem yourself. 2Finally, remember that I said that there are differences between arrow and regular functions. The case of 2 is one of them.For an arrow function, the value depends on the lexical scope – that is to say, the outer function where the arrow function is declared. So, if we make the 04 from above an arrow function, nothing will work.Arrow functions basically inherit the parent's context which in the above case is the 1.Prototypes and Prototypal Inheritance in JavaScript 👪
All this comes via 06. 07 is the object where JS is putting it all.Let's see some examples. Fire up your consoles! 3All this is called a 08.We can do the same with objects and functions as well. We will always find 09 behind the scenes. That's why you may have heard that everything in JS is an object. 🤯What is Prototypal Inheritance in JavaScript? 4Note: Don't modify prototypes this way. It's just for understanding. Here's the right way to do it. 5By doing this, 10 gets access to the object's properties. So, now we can do: 6This is prototypal inheritance. Asynchronous JavaScript ⚡So, JS is a single-threaded language. Things happen one at a time. Only after one thing is done can we move to the next thing. But this creates problems in the real world, especially, when we are working with browsers. For example, when we need to fetch data from the web - often times we don't know how long will it take to get it. And whether we will be able to get the data successfully. To help with this, asynchronous JS comes into play. And the most important concept to understand is the event loop. Event Loops in JavaScript ➰Instead of providing a half-baked explanation here, I highly recommend watching this video by Philip Roberts if you haven't already: Learn all about event loops in JS here. Timers in JavaScript – setTimeout, setInterval, clearInterval ⏱️I hope you watched the video. It mentioned timers. Let's talk about them more now. These are very frequently asked about in interviews. The 11 method calls a function or evaluates an expression after a specified number of milliseconds. 12 does the same for specified intervals. 7You use 13 to stop the timer. 8Let's go over some questions that use these concepts. 9Here's a slightly trickier one: 0And here's a short explanation of what's going on there: when 14 comes again into the picture, the entire loop has run and the value of 15 has become 6,Now, let's say we want the outcome to be 1 2 3 4 5 – what do we do? Instead of 9 ➡️ use 0.Why this will work? 9 is globally scoped but 0 is locally scoped. So for 0 a new 15 is created for every iteration.Promises in JavaScript (❗important) 🤝Promises are at the heart of Asynchronous JS.
A promise can be in one of these three states:
1Note: 22 and 23 are just conventional names. Call it pizza🍕 if you like.Instead of 24, we can also use 25: 2One of the advantages of promises is that they are a much cleaner syntax. Before we had promises, we could easily get stuck in callback hell 🌋 Advanced JavaScript Concepts to Know📚 Polyfills in JavaScript
3Notice how we use 2. Here, we have basically created a new array and are adding values to it.Async and defer in JavaScript ✔️These concepts are frequently asked about in interviews by big corporations like Amazon, Walmart, and Flipkart. 🏢 To understand 28 and 29, we need to have an idea of how browsers render a webpage. First, they parse the HTML and CSS. Then DOM trees are created. From these, a render tree is created. Finally, from the render tree - a layout is created and the painting happens.For a more detailed look, check out this video. Async and defer are 30 attributes which can be loaded along with the script tags. They are useful for loading external scripts into your web page.Let's understand with the help of pictures. If there are multiple scripts which are dependant on each other, use 29. Defer script are executed in the order which they are defined.If you want to load external script which is not dependant on the execution of any other scripts, use 28.Note: The async attribute does not guarantee the order of execution of scripts. Debouncing in JavaScript ⛹️♂️Debouncing is another favorite topic of interviewers. Let's understand it by creating a search bar. Demo: https://codesandbox.io/s/debounce-input-field-o5gml Create a simple input field in 33 like this: 4Now, in 34. Don't forget to add it to 33 first: 5First, we have selected the input and added an 36 to it. Then we created a debounce function which takes a callback function and delay.Now, inside the debounce function we create a timer using 14. Now, this timer's job is to make sure that the next call for 38 only happens after 300 ms. This is what debouncing is.Also, we use 39 to remove it. Don't want too many of them hanging out there taking up memory space!Phew! Lots of theory. Let's do a fun challenge. You must have seen the countdown before a game starts (it goes like 10, 9, 8, .... with some delay in between). Try to write a program for it. Here's how you'd do it: 6Were you able to solve it? Did you do it differently? Let me know your solution. Throttling in JavaScript 🛑Let's look at an example again. Suppose that on every window resize event we call an expensive function. Now, we want it such that the expensive function will only be executed once in the given time interval. This is what throttling is. Create an 33 and an 34 with the following code: 7Almost the same as debouncing. The key difference is the 42 variable. Only, when it's true we are invoking the callback function. And it is set to 43 inside the 14. So the value is 43 only after the desired time limit.So, what's the difference between debounce and throttling❓Let's take the search bar 🔍 example from above. When we are debouncing the input field, we are saying to only fetch the data when the difference between two 46 events is at least 300 ms.In the case of throttling, we make a function call only after a certain period of time. Suppose that you are searching for an encyclopedia in the search bar. The first call is made on 47 and it took us 300 ms to reach 48. The next call will be made then only. All the events in between will be ignored.So, to summarize, debouncing is when the difference between two 46 events is 300 ms. And throttling is when the difference between two function calls is 300 ms. Basically, the function is called after a certain interval of time.Storage in JavaScript 💾Finally, a small but important topic to wrap things up. localStorage: Data persists even after closing your session sessionStorage: You lose your data when your session is over, like when you close the browser on the tab. 8And we are done! 🏁 I hope you feel more confident about your next JS interview now. I wish you all the best. If you have any queries / suggestions / feedback, you can reach me on Twitter: https://twitter.com/rajatetc. 🗃️ Main References
ADVERTISEMENT ADVERTISEMENT ADVERTISEMENT ADVERTISEMENT ADVERTISEMENT ADVERTISEMENT ADVERTISEMENT ADVERTISEMENT ADVERTISEMENT ADVERTISEMENT ADVERTISEMENT ADVERTISEMENT ADVERTISEMENT ADVERTISEMENT ADVERTISEMENT ADVERTISEMENT ADVERTISEMENT ADVERTISEMENT software engineer, unacademy • rajatgupta.xyz • twitter.com/rajatetc • development, design, psychology, and startups If you read this far, tweet to the author to show them you care. Tweet a thanks Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started |