Js Practice Interview Questions
Tags: javascript, node, interview
Published: 2022-10-12 16:52:39

Hello,
My name is Viktor, I’m from Ukraine and I’m Node.js Backend Engineer.
Today I’m going to write my first post on dev.to, so please don't criticize me too much.

In my company from time to time I'm involved in the interview of candidates, mostly about Node.js , AWS, Serverless stack.

Sometimes in order to evaluate a candidate's skills, I may ask about the output of the code. As my experience shows huge number of candidates know the theory very well but once I show the code, 90% can not give the right answer.That's why I decided to evaluate some parts/areas by showing the code instead of asking about theory.

In this post I’m going to provide you with 5 examples that I ask candidates.

**1. Arrow functions**
```js 
const obj = {
  name: "Viktor",
  getName: () => this.name
} 

console.log(obj.getName()) // result ? 
```

```
Wrong Answer: "Viktor"
Right Answer: undefined
```

***Explanation***
Arrow functions do not bind their own _**this**_, instead, If _**this**_ is accessed, it is taken from the outside, they inherit the one from the parent scope, which is called "lexical scoping”. If you use _**this**_ in the global context, it will reference the window object in the browser and the global object in the node.

As one possible solution to fix this, just use a regular function

```js 
const obj = {
  name: "Viktor",
  getName: function () {
   return this.name
  }
} 
```

**2. Const declaration**

```js 
const score = 10;
if (score < 100) {
    const result = "Not Passed";
}
console.log('result', result);
```

```
Wrong answer: "Not Passed”, undefined, null
Right Answer: ReferenceError: result is not defined
```


***Explanation***
Constants are block-scoped.
So, what is a block? A block (that is, a code block) is a section of the code we define using a pair of curly brace s({...}). Something like this:

```js
//outer block scope
{//inner block-scope
  const name = "Viktor";
}
//outer block scope
```
All variables declared with _**const**_ have block level scope, limiting its scope to the statement or expression from within which it is declared - so in our case outer scope cannot read values defined in an inner scope (except when defined with var).





**3. setImmediate() vs setTimeout()**

```js 
// timeout_vs_immediate.js
setTimeout(() => {
  console.log('timeout');
}, 0);

setImmediate(() => {
  console.log('immediate');
});
```

```
$ node timeout_vs_immediate.js
```
What result will be printed?  
```
Wrong answer:
timeout
immediate

AND

immediate
timeout

Right Answer: The output of the above is not predictable
```

***Explanation:***
The order in which the timers are executed will vary depending on the context in which they are called. If both are called from within the main module(is not within an I/O cycle), then the order in which the two timers are executed is non-deterministic, as it is bound by the performance of the process.

However, if you move the two calls within an I/O cycle, the immediate callback is always executed first:
```js
// timeout_vs_immediate.js
const fs = require('fs');

fs.readFile(__filename, () => {
  setTimeout(() => {
    console.log('timeout');
  }, 0);
  setImmediate(() => {
    console.log('immediate');
  });
});
```

```
$ node timeout_vs_immediate.js
immediate
timeout
```

**4. Closures Inside Loops**

```js
for(var i = 0; i < 10; i++) {
    setTimeout(function() {
      console.log(i);  
    }, 10);
}
```
What will be logged?
```
Wrong answer: 0,1,2,3,4,5,6,7,8,9
Right answer: 10,10,10,10,10,10,10,10,10,10 //ten 10 times
```
***Explanation:***
The console log is inside the async _**setTimeout**_ function and _**setTimeout**_ will be executed when the current stack is over. So, the loop finishes and _**setTimeout**_ is being executed. Since, the loop is already finished, the value _**i**_ has been set to 10. When setTimeout use the value of _**i**_ by reference, it gets the value of _**i**_ as 10, so based on it will be logged _**10**_ ten times.


**5. ..toString()**

```js
2022..toString() // result ?
```
```
Wrong answer: SyntaxError: Invalid or unexpected token
Right answer: "2022"
```

***Explanation:***
First dot is actually a decimal point and the second dot invokes property or method, so _**2022..toString()**_ it's a short version of _**2022.0.toString()**_

---


I hope it was interesting for you and you have learned something new.

Cheers,
Viktor