Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
October 19, 2020 01:17 am GMT

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.

Implementation

class Stack {    constructor() {        this.top = -1;        this.store = [];    }    pop = () =>{        if(this.top<0){            return null;        }        let poppedItem = this.store[this.top];        this.store.length = --this.top+1;        return poppedItem;    }    push = (item)=>{        this.store[++this.top] = item;        return;    }    getTop = ()=>{        return this.store[this.top];    }}let myStack = new Stack();myStack.push("10");myStack.push("34");myStack.push("17");console.log(myStack.getTop());//output 17console.log(myStack.pop());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.
implementation

var add = (function(){    let accumulator = 0;    return function(value){        return accumulator+=value;    }})();console.log(add(3)); //output 3console.log(add(5)); //output 8console.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

console.log("start");new Promise((resolve,reject)=>{    resolve({data:'hello world'});}).then(res=>{    console.log(res);})console.log("end");//outputs//start//end//{ 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!");}setTimeOut(logger,2000);
Enter fullscreen mode Exit fullscreen mode

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

Conclusion

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


Original Link: https://dev.to/dverybest/basic-javascript-interview-questions-3491

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To