Real JavaScript Interview questions
Tags: javascript, programming, eventloop, webdev
Published: 2022-08-09 22:37:00

Following are real questions asked to me in an interview. Of course there are many others concepts that can be asked. I hope this is useful for you guys.

Questions are divided in two different sections:
1. Theory
2. Code Snippets questions


## 1/2 Theory


## What do you know about Primitive Data Types?

It’s a type of data that represents a single value. That is, not an object. Following are the primitive data types in JavaScript:

`String`: a sequence of characthers in ‘single’ or “double quotes”

`Number`: integers or floating point number

`boolean`: only `true` or `false` values

`undefined`: represents lack of existence, a variable that has not been assigned any value. As developer, you should **never** set a variable to `undefined` directly.

`null` : also represents lack of existence. You can set a variable to `null`

## What do you know about Objects?

Objects are name value pairs that are sitting in memory and could be compound of properties and methods. The most common way to create an object is using _Object literal_ syntax:

```jsx
const person = {
	name: 'John',
	lastname: 'Doe',
	address: {
		street: '101 Sherman Ave',
		city: 'New York City',
		state: 'New York'
	}
}
```

## What is the `Object.prototype` ?

It is mechanish where you can find other methods inherited from other. For instance, when you create a new Date, you can find methods like `getYear()` in this prototype chain.

## What do you know about Arrays?

Arrays are a type of object for storing collections in a single variable. Arrays can hold a mix of anything: functions, primitives, objects.

## How does JavaScript works?

Let’s take a look to following image and explain how Event Loop works:

![Event Loop image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2hz7z61c1yr5vrohou9f.png)

- Call Stack (blue block):
    - It is the stack where all the code task will be stacking: Last In First Out. JavaScript can only execute one task at the time.
    - Single tasks are executed but when we have other kind of tasks, like `setTimeOut` , `setInterval` or `fetch` the Call Stack delegates it to the Web APIs.
- Web APIs (purple block):
    - It is a set of tools that browsers make available. It is responsible for executing timers (for `setTimeOut` or `setInterval`) or trigger HTTP request for fetching data.
    - Once the timer is completed or the HTTP request has a response, it will transfer the task to the Task Queue o Micro Task Queue.
- Task Queue (yellow block):
    - It contains all the tasks pending to execution. Internally this tasks has a priority depending on what type they are. From high to low priority, tasks types could be: Render, DOM Tasks, UI Tasks, Net Tasks.
    - Tasks on this queue are not executed. They are only waiting to be transfer by the Event Loop to Call Stack and there they will be executed.
- Micro Tasks Queue (aqua block):
    - When a Promise has a response, either resolved or rejected, its tasks are moved to Micro Tasks Queue.
    - Tasks on this queue are not executed. They are only waiting to be transfer by the Event Loop to Call Stack.
    - Tasks on this queue has a **higher priority** than tasks on the Task Queue, even if tasks on Tasks Queue are Render type.
- Event Loop (green block):
    - It is a loop responsible for taking tasks from Tasks Queue/Micro Tasks Queue to Call Stack.
    - It only transfers a task to the Call Stack, if the Call Stack is empty.
    - If the Call Stack is empty, and there is one task in Micro Task Queue and another task in Task Queue, Event Loop will transfer first the task on the Micro Tasks due its priority. The remaining task will be moved once the Call Stack is empty again.

## Differences between `let` and `const`

- `let`:
    - is block scope
    - it can be updated
    - it can be declared without initialization
- `const`:
    - is block scope
    - it cannot be updated
    - it cannot be updated without initialization
    

## Differences between `map` and `forEach` ?

- The main difference is that `map` returns a new array by applying the callback function
- You can use `forEach` just for iterate

## 2/2 Code snippets

If you execute following code, what does it will retrieve?

```jsx
typeof {} === typeof []
```

- Response: `true`

Data type for Objects and Arrays is `object` . Which means that the type of an empty object will be equals to the type of an

---

If you execute following code, what name does it will console?

```jsx
const user = {
	name: "name1"
};
const user2 = user;
user2.name = "name2";

console.log(`user: ${JSON.stringify(user)}`);
```

- Response: It prints the user with name: `name2`  

This is because in JavaScript Primitive types are passed by Value and non Primitive types, like objects, are passed by Reference

---

If you execute following code, what will be the output?

```jsx
setTimeout(() =>{
	console.log("Lorem ipsum");
});

const promise1 = Promise.resolve(123);
promise1.then((value) => {
  console.log(`Promise value: ${value}`);
});

console.log("Hello")
```

- Response:

```
Hello
Promise value
Lorem ipsum
```

This is related about we previously checked with the Event Loop. Tasks created as a response for a `Promise` will be queue in the Micro Tasks Queue and they will have a higher priority than tasks on the Task Queue.

---

I hope you find useful these questions if you are getting ready for an interview. And as always, the best luck!