DEV Community

Cover image for Basic JavaScript Interview Questions
Charles Best
Charles Best

Posted on • Updated on

Basic JavaScript Interview Questions

Hi everyone! So I had couple of interviews this year for JavaScript software development roles, and I felt its worth writing an article on some of the interview questions I was asked.
In this article I will write some of the questions I was asked and the answers to them.

Explain data structure

Data structure is a data organization, management, and storage format that enables efficient access and modification. A data structure is a collection of data values, the relationships among them, and the functions or operations that can be applied to the data.

Simply put, data structure is a defined format/way of storing and managing a collection of data.

List examples of data structures, explain and implement one

some common data structures includes:

  • array
  • linked list
  • double linked list
  • stack
  • queue
  • hash map
  • etc

I will explain and implement a stack.

A stack is a linear data structure that stores data in a LIFO(Last in First Out) manner, i.e. the item added/inserted last is the first item to be accessed. Data in a stack can only be accessed from one end(top of the stack).
A Stack data structure supports only two type of operation namely PUSH(insert/add item) and POP(delete/remove item) operation.


class Stack {
    constructor() { = -1; = [];

    pop = () =>{
            return null;
        let poppedItem =[]; =;
        return poppedItem;

    push = (item)=>{[] = item;

    getTop = ()=>{

let myStack = new Stack();

console.log(myStack.getTop());//output 17
console.log(myStack.getTop());//output 34
Enter fullscreen mode Exit fullscreen mode

Explain Closure with code example

A closure is a function having access to the parent scope, even after the parent function has closed.

var add = (function(){
    let accumulator = 0;
    return function(value){
        return accumulator+=value;

console.log(add(3)); //output 3
console.log(add(5)); //output 8
console.log(add(7)); //output 15
Enter fullscreen mode Exit fullscreen mode

Closure makes it possible for functions to have private variable. E.g in the code above, the function returned by the anonymous function is still able to access the accumulator variable even though the anonymous function is done executing.

Explain Asynchronicity in JavaScript with code example

JavaScript is single threaded, meaning codes are executed sequentially/synchronously(line by line one after the other). Asynchronous JavaScript enables code execution without blocking the main thread, i.e code execute without blocking/stopping other code from executing immediately while its still running/executing.

code example

new Promise((resolve,reject)=>{
    resolve({data:'hello world'});
//{ data: 'hello world' }
Enter fullscreen mode Exit fullscreen mode

In the code above, console.log("end") executes before the promise even though the promise started executing first. This is because the promise is asynchronous and did not block the main thread allowing console.log("end") to execute while it is executing.

Explain Higher Order Functions.

Higher order functions are functions that take other functions as arguments and/or a function that returns a function.
code example

function logger(){
console.log("Hello world!");
Enter fullscreen mode Exit fullscreen mode

In the above setTimeOut is a higher other function that takes the function logger as an argument.


I hope you find this article useful and it helps you prepare for interview.
If you like the content, feel free to stay in touch, follow me on twitter

Top comments (14)

recursivefaults profile image
Ryan Latta

One alternative to the stack example is to use shift/unshift.

A benefit is it covers adding and removing items, and allows you to treat the index of 0 as the top without worrying about it.

Also, not many people pay attention to those methods so you might even come across as clever for having non-obivous knowledge of JS Arrays.

jperasmus profile image
JP Erasmus

Using shift/unshift for the stack would not be a very efficient solution when using JS arrays under the hood because that would turn an O(1) action into O(n) since every item in the array would have to update its index every time to perform either of these actions. Pushing onto the end or popping from it is O(1) since only a single item is affected.

recursivefaults profile image
Ryan Latta

Being able to talk about that makes a better interview.

grglldz profile image

Closure is not necessarily a function, it can be any data type.
Closure is just a reference that is in the accessible scope, whose value comes from a scope that is not accessible.

piotrlewandowski profile image
Piotr Lewandowski

"Closure is not necessarily a function, it can be any data type" not, exactly. According to MDN: "A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment)." (source:

grglldz profile image

That is not wrong. That's just one aspect of closures.

Thread Thread
piotrlewandowski profile image
Piotr Lewandowski

It's wrong when you phrase it like "JUST a reference that" ("just" suggests it's only that, not "one aspect of") when it's clearly not "just a" :) That's what I meant ;)

darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

Wrong. A closure is the combination of a function and some references to either values or variables, depending on the language.

It's commonly simplified to just a function that has access to it's calling scope, because it effectively behaves that way in javascript and pretty much any other language that has them.

drkvogel profile image
Chris Bird

They are things in computer science, sure, but perhaps not used in frontend development very much. Certainly "call stack" and "Memory Heap" referenced by the OP are things that exist, but are not really necessary to do frontend web development. Backend is a different matter...

great-dev profile image
Great Dev

Hi Charles!
My name is Serhii and I am from Ukraine.
I have just read your post carefully. I see you are very good at web developing.
As a beginner, I really want to learn from an excellent dev like you and make a career of mine.
If you are interest in my desire, please contact me,

sonnuk profile image

Good Job! Thank you for sharing
you explained concepts with examples, it's good.
Get Some more topics about javaScript:

darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️ • Edited

You probably (indirectly) use Hashmaps quite often: V8 uses them to represent objects when it can't find a good way to optimize their layout.

dverybest profile image
Charles Best

These may not be something you implement/use in your everyday web development, but understanding them can mean you have basic understanding of code execution in JavaScript. Things like the "call stack", "Memory Heap", "callback queue" and "event loop".

darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️ • Edited

Try saying Asynchronicity five times

By the way: code is uncountable when referring to source code. There is no "codes".