10 JavaScript Interview Questions
Tags: javascript, webdev, codenewbie, beginners
Published: 2020-11-20 16:27:13

**Note:** Every question is followed by **Output** and a rough **Explanation**. Scroll at your own risk.

## Q1.

```jsx
var msg = "hello";

if (true) {
  var msg = "welcome";
}
console.log(msg);

// ----

let msg2 = "hello";

if (true) {
  let msg2 = "welcome";
}
console.log(msg2);
```

### Output

```jsx
welcome
hello
```

### Explanation

`var` is function scoped, therefore, when `msg` is declared inside the `if` block, it overrides the `msg` in the global scope. This does not happen with `let` as it is block scoped.

## Q2.

```jsx
for (var i = 0; i < 5; i++) {
  setTimeout(function () {
    console.log(i);
  }, 1000);
}
```

### Output

```jsx
5
5
5
5
5
```

### Explanation

Since `var` is function scoped the `i` variable holds the value `5` after the loop ends. The callback function in the `setTimeout` gets the same value for every occurrence.

### Solution

- Convert `var` to `let` to create a scope for every iteration.

```jsx
for (let i = 0; i < 5; i++) {
  setTimeout(function () {
    console.log(i);
  }, 1000);
}
```

- Wrap the `setTimeout` in an anonymous function. Passing `i` as a parameter scopes it to the anonymous function and therefore the value is not lost.

```jsx
for (var i = 0; i < 5; i++) {
  (function (x) {
    setTimeout(function () {
      console.log(x);
    }, 1000);
  })(i);
}
```

## Q3.

```jsx
const obj = {
  ["foo_" + (() => 1)()]: "Hello",
};
console.log(obj);
```

### Output

```jsx
{ foo_1: 'Hello' }
```

## Q4.

```jsx
console.log([1,2,3] + [4,5,6]);
```

### Output

```jsx
1,2,34,5,6
```

### Explanation

`String([1,2,3]);` is `"1,2,3"`

Hence, `"1,2,3" + "4,5,6"` is `"1,2,34,5,6"`

## Q5. What's the order of execution?

```jsx
console.log('1');
setTimeout(() => console.log('2'), 0);
Promise.resolve().then(() => console.log('3'));
console.log('4');
```

### Output

```jsx
1
4
3
2
```

### Explanation

**Priority for event loop is: call stack > microtask > macrotask**

All the synchronous code is executed first

Therefore, logs `1`, `4`

Next, there is a `Promise` and a `setTimeout`

Promise callback is stored in `microtask queue` & setTimeout callback is stored in `macrotask queue`

`microtask` has a higher priority over `macrotask`. Therefore, it logs `3`  followed by `2`

## Q6.

```jsx
console.log(typeof typeof 1);
```

### Output

```jsx
string
```

### Explanation

Evaluate from Right to Left

1. `typeof 1` return number
2. `typeof 'number'` returns string

## Q7.

```jsx
console.log(Math.max() < Math.min());
```

### Solution

```jsx
true
```

### Explanation

The default value for `Math.max()` is `-Infinity` & default value for `Math.min()` is `Infinity`

Hence, `-Infinity < Infinity` is `true`

## Q8.

```jsx
function func() {
  return foo;

  foo = 1;
  function foo() {}
  var foo = "hello";
}

console.log(typeof func());
```

### Output

```jsx
function
```

### Explanation

Due to one round of parsing (hoisting) the code looks like this

```jsx
function func() {
  var foo; // hoisted
  function foo() {} // hoisted
  return foo;

  foo = 1;
  foo = "hello";
}

console.log(typeof func());
```

Because of that, the last value available for foo is `function`

## Q9.

```jsx
console.log(3 > 2 > 1);
```

### Output

```jsx
false
```

### Explanation

It starts from left to right
so `3 > 2` equals `true`

`true > 1` is equivalent to `1 > 1` which is `false`

## Q10.

```jsx
function fn() {
  return this.str;
}

var str = "global";

var obj = {
  str: "obj",
};

console.log(fn(), fn.call(obj));
```

### Output

```jsx
global obj
```

### Explanation

When executing `fn()` the value of `this` is `window` and `window.str` is `global`.

`.call()` assigns `this` to `obj` and `obj.str` is `obj` 

**Note**: *This solution works in `non-strict` mode.*

---

**Thanks for reading 💙**  
Follow **@codedrops.tech** for daily posts.  
[Instagram](https://www.instagram.com/codedrops.tech/) ● [Twitter](https://twitter.com/codedrops_tech) ● [Facebook](https://www.facebook.com/codedrops.tech/)

Micro-Learning ● Web Development ● Javascript ● MERN stack ● Javascript  
**[codedrops.tech](https://www.codedrops.tech/)**