Js Practice Interview Questions

vkkis93

Viktor Kis

Posted on October 12, 2022

Js Practice Interview Questions

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

const obj = {
  name: "Viktor",
  getName: () => this.name
} 

console.log(obj.getName()) // result ? 
Enter fullscreen mode Exit fullscreen mode
Wrong Answer: "Viktor"
Right Answer: undefined
Enter fullscreen mode Exit fullscreen mode

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

const obj = {
  name: "Viktor",
  getName: function () {
   return this.name
  }
} 
Enter fullscreen mode Exit fullscreen mode

2. Const declaration

const score = 10;
if (score < 100) {
    const result = "Not Passed";
}
console.log('result', result);
Enter fullscreen mode Exit fullscreen mode
Wrong answer: "Not Passed”, undefined, null
Right Answer: ReferenceError: result is not defined
Enter fullscreen mode Exit fullscreen mode

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:

//outer block scope
{//inner block-scope
  const name = "Viktor";
}
//outer block scope
Enter fullscreen mode Exit fullscreen mode

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()

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

setImmediate(() => {
  console.log('immediate');
});
Enter fullscreen mode Exit fullscreen mode
$ node timeout_vs_immediate.js
Enter fullscreen mode Exit fullscreen mode

What result will be printed?





Wrong answer:
timeout
immediate

AND

immediate
timeout

Right Answer: The output of the above is not predictable
Enter fullscreen mode Exit fullscreen mode

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:

// timeout_vs_immediate.js
const fs = require('fs');

fs.readFile(__filename, () => {
  setTimeout(() => {
    console.log('timeout');
  }, 0);
  setImmediate(() => {
    console.log('immediate');
  });
});
Enter fullscreen mode Exit fullscreen mode
$ node timeout_vs_immediate.js
immediate
timeout
Enter fullscreen mode Exit fullscreen mode

4. Closures Inside Loops

for(var i = 0; i < 10; i++) {
    setTimeout(function() {
      console.log(i);  
    }, 10);
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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()

2022..toString() // result ?
Enter fullscreen mode Exit fullscreen mode
Wrong answer: SyntaxError: Invalid or unexpected token
Right answer: "2022"
Enter fullscreen mode Exit fullscreen mode

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

💖 💪 🙅 🚩
vkkis93
Viktor Kis

Posted on October 12, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related