Think You're a JavaScript Pro? Find Out with this Quiz👇
Tags: webdev, javascript, beginners, programming
Published: 2023-02-12 12:22:38

### Q1. How is a forEach statement different from a for statement?

1. Only a for statement uses a callback function.

2. A for statement is generic, but a forEach statement can be used only with an array.

3. Only a forEach statement lets you specify your own iterator.

4. A forEach statement is generic, but a for statement can be used only with an array.

{% details Answer %} 2 {% enddetails %}

[Reference Differences between forEach and for loop](https://www.geeksforgeeks.org/difference-between-foreach-and-for-loop-in-javascript/)

---

### Q2. When would the final statement in the code shown be logged to the console? When would 'results shown' be logged to the console?

```js
let modal = document.querySelector('#result');
setTimeout(function () {
  modal.classList.remove('hidden');
}, 10000);
console.log('Results shown');
```

1. after 10 second
2. after results are received from the HTTP request
3. after 10000 seconds
4. immediately

{% details Answer %} 4 {% enddetails %}

[Reference Javascript is synchronous and single threaded](https://stackoverflow.com/a/2035662/15067394)

---

### Q3. Which snippet could you add to this code to print "food" to the console?

```js
class Animal {
  static belly = [];
  eat() {
    Animal.belly.push('food');
  }
}
let a = new Animal();
a.eat();
console.log(/* Snippet Here */); //Prints food
```

1. `a.prototype.belly[0]`
2. `Object.getPrototype0f (a).belly[0]`
3. `Animal.belly[0]`
4. `a.belly[0]`

{% details Answer %} 3 {% enddetails %}

[Reference Javascript Class static Keyword](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/static)

---

### Q4. You've written the code shown to log a set of consecutive values, but it instead results in the value 5, 5, 5, and 5 being logged to the console. Which revised version of the code would result in the value 1, 2, 3 and 4 being logged?

`A`

```js
for (var i = 1; i <= 4; i++) {
  setTimeout(function () {
    console.log(i);
  }, i * 10000);
}
```

`B`

```js
for (var i = 1; i <= 4; i++) {
  (function (i) {
    setTimeout(function () {
      console.log(j);
    }, j * 1000);
  })(j);
}
```

`C`

```js
for (var i = 1; i <= 4; i++) {
  setTimeout(function () {
    console.log(i);
  }, i * 1000);
}
```

`D`

```js
for (var i = 1; i <= 4; i++) {
  (function (j) {
    setTimeout(function () {
      console.log(j);
    }, j * 1000);
  })(i);
}
```

`E`

```js
for (var j = 1; j <= 4; j++) {
  setTimeout(function () {
    console.log(j);
  }, j * 1000);
}
```

{% details Answer %} D {% enddetails %}

1. [Reference setTimeout](https://developer.mozilla.org/en-US/docs/Web/API/setTimeout)
2. [Reference immediately invoked anonymous functions](https://www.javascripttutorial.net/javascript-anonymous-functions/)

---

### Q5. How does a function create a closure?

1. It reloads the document whenever the value changes.
2. It returns a reference to a variable in its parent scope.
3. It completes execution without returning.
4. It copies a local variable to the global scope.

{% details Answer %} 2 {% enddetails %}

[Reference](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures)

---

### Q6. Which statements(multiple options) creates a new function called discountPrice?

`A`

```js
let discountPrice = function (price) {
  return price * 0.85;
};
```

`B`

```js
let discountPrice(price) {
  return price * 0.85;
};
```

`C`

```js
let function = discountPrice(price) {
  return price * 0.85;
};
```

`D`

```js
discountPrice = function (price) {
  return price * 0.85;
};
```

{% details Answer %} A & D {% enddetails %}

[Reference defining javascript functions](https://www.w3schools.com/js/js_functions.asp)

---

### Q7. What is the result in the console of running the code shown?

```js
var Storm = function () {};
Storm.prototype.precip = 'rain';
var WinterStorm = function () {};
WinterStorm.prototype = new Storm();
WinterStorm.prototype.precip = 'snow';
var bob = new WinterStorm();
console.log(bob.precip);
```

1. Storm()
2. undefined
3. 'rain'
4. 'snow'

{% details Answer %} 4 {% enddetails %}

[Reference prototype chain](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain)

---

### Q8. What is the result in the console of running this code?

```js
'use strict';
function logThis() {
  this.desc = 'logger';
  console.log(this);
}
new logThis();
```

1. `undefined`
2. `window`
3. `{desc: "logger"}`
4. `function`

{% details Answer %} 3 {% enddetails %}

[Reference javascript classes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes)

---

### Q9. You're adding error handling to the code shown. Which code would you include within the if statement to specify an error message?

```js
function addNumbers(x, y) {
  if (isNaN(x) || isNaN(y)) {
  }
}
```

1. `exception('One or both parameters are not numbers')`
2. `catch('One or both parameters are not numbers')`
3. `error('One or both parameters are not numbers')`
4. `throw('One or both parameters are not numbers')`

{% details Answer %} 4 {% enddetails %}

[Reference javascript throw](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/throw)

---

### Q10. When would you use a conditional statement?

1. When you want to reuse a set of statements multiple times.
2. When you want your code to choose between multiple options.
3. When you want to group data together.
4. When you want to loop through a group of statement.

{% details Answer %} 2 {% enddetails %}

[Reference javascript conditionals](https://www.javascript.com/learn/conditionals)

---

### Q11. Which Object method returns an iterable that can be used to iterate over the properties of an object?

1. `Object.get()`
2. `Object.loop()`
3. `Object.each()`
4. `Object.keys()`

{% details Answer %} 4 {% enddetails %}

[Reference javascript object static methods](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object#static_methods)

---

### Q12. What is one difference between collections created with Map and collections created with Object?

1. You can iterate over values in a Map in their insertion order.
2. You can count the records in a Map with a single method call.
3. Keys in Maps can be strings.
4. You can access values in a Map without iterating over the whole collection.

{% details Answer %} 2 (`Map.prototype.size returns the number of elements in a Map, whereas Object does not have a built-in method to return its size.`) {% enddetails %}

[Reference map methods javascript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map)

---

#### Q13. 0 && hi

1. ReferenceError
2. true
3. 0
4. false

{% details Answer %} 3 {% enddetails %}

[Reference boolean logic](https://ntgard.medium.com/https-medium-com-ntgard-boolean-logic-in-javascript-part-1-3371af974f19)

---

### Q14. Which of the following values is not a Boolean false?

1. `Boolean(0)`
2. `Boolean("")`
3. `Boolean(NaN)`
4. `Boolean("false")`

{% details Answer %} 4 {% enddetails %}

[Reference boolean of a string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)

---

### Q15. For the following class, how do you get the value of 42 from an instance of X?

```js
class X {
  get Y() {
    return 42;
  }
}
var x = new X();
```

1. `x.get('Y')`
2. `x.Y`
3. `x.Y()`
4. `x.get().Y`

{% details Answer %} 2 {% enddetails %}

[Reference getters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get)

---

Post your result in the comment section and let me know how was the quiz🙌👍