Intermediate JavaScript test (Intermediate)
30 questions · 35 minutes · Free
Closures, prototypes, the event loop and async patterns.
Take the Intermediate JavaScript test
What it covers
- Closures and scope
- this and prototypes
- Event loop and microtasks
- Promises and async/await
- ES modules
Interview format
- Questions: 30 questions — 18 multiple choice, 9 multi-answer, 3 predict-the-output
- Time: 35 minutes, timed from the first question
- Difficulty: Intermediate
- Skill area: Frontend
- Price: Free
Earn a certificate
The Intermediate JavaScript test is one of the 6 tests of the Frontend Developer learning path. Pass it and the other 5 tests at 70% or more and readyforhires issues you a verifiable certificate in your name, with its own code and verify link to share or add to LinkedIn.
Sample questions
5 sample questions from the Intermediate JavaScript test, with the answers. Samples are taken out of the live test, so you will not meet these when you sit it.
Which of the following correctly describes the behavior of Promise.all() when handling multiple promises in a project scenario?
- The Promise.all() method continues processing even if one promise is rejected, and only rejects if all promises are rejected.
- The Promise.all() method waits for all promises to resolve before proceeding, and if any promise is rejected, it immediately rejects with that error.
- The Promise.all() method resolves immediately if any promise is resolved, regardless of other promises.
- The Promise.all() method ignores any rejected promises and only considers resolved ones.
Answer: B. The Promise.all() method waits for all promises to resolve before proceeding, and if any promise is rejected, it immediately rejects with that error.
Promise.all() is designed to handle multiple promises at once. It only resolves when all promises in the iterable have resolved successfully. If any of the promises is rejected, it immediately rejects with that error, without waiting for the other promises to resolve. This makes it useful for scenarios where all asynchronous operations must complete successfully for the overall operation to succeed.
Consider the following code: <br><br><button id='myButton'>1</button><br><script>const button = document.getElementById('myButton');button.value = '3';button.addEventListener('click', function() { setTimeout(() => { console.log(this.value); }, 1000); });</script><br><br>What will the code log after the button is clicked and the timeout completes?
- The function will throw an error.
- The function will log '3' after the timeout completes.
- The function will log '1' after the timeout completes.
- The function will log 'undefined' after the timeout completes.
Answer: B. The function will log '3' after the timeout completes.
In JavaScript, when a function is used as an event handler, the value of 'this' inside the function refers to the element that triggered the event, not the outer scope. In this case, the 'this' inside the timeout callback refers to the button element, and 'this.value' correctly retrieves the value of the button, which is '3'.
Consider the following code: function createFunctions() { const result = []; for (var i = 0; i < 5; i++) { result.push(function() { console.log(i); }); } return result; } const functions = createFunctions(); functions.forEach(func => func()); What will be the output of this code?
- The function will log 5, 5, 5, 5, 5.
- The function will log an error.
- The function will log 1, 2, 3, 4, 5.
- The function will log 0, 1, 2, 3, 4.
Answer: A. The function will log 5, 5, 5, 5, 5.
The closure captures the variable 'i' by reference, not by value. Since the loop completes before any of the functions are executed, all functions refer to the final value of 'i', which is 5.
Consider the following code snippet: function createFunctions() { let results = []; for (let i = 0; i < 3; i++) { results.push(function () { console.log(i); }); } return results; } let functions = createFunctions(); functions[0](); functions[1](); functions[2](); What will be the output of this code?
- The function will log 3, 3, 3
- The function will not execute because of a syntax error
- The function will log 1, 2, 3
- The function will log 0, 1, 2
Answer: A. The function will log 3, 3, 3
In JavaScript, closures capture variables by reference, not by value. When the loop runs, the variable 'i' is updated, and all the functions created in the loop share the same 'i' variable. As a result, when the functions are executed after the loop completes, they all log the final value of 'i', which is 3.
Consider the following code: `for (let i = 0; i < 3; i++) { const f = () => console.log(i); } f();`. What will happen when the function `f` is called after the loop completes?
- The function `f` will be redeclared in each iteration, and the closure will capture the same value of `i` for all iterations due to the `let` declaration.
- The function `f` will be redeclared in each iteration, but the closure will not capture any value of `i` due to the `let` declaration.
- The function `f` will be redeclared in each iteration, but the closure will capture the initial value of `i` due to the `let` declaration.
- The function `f` will be redeclared in each iteration, but the closure will capture the latest value of `i` due to the `let` declaration.
Answer: D. The function `f` will be redeclared in each iteration, but the closure will capture the latest value of `i` due to the `let` declaration.
In the loop, `let i` creates a new binding for `i` in each iteration. The closure inside the function `f` captures the current value of `i` for each iteration. Even though the function is redeclared in each iteration, the closure correctly captures the latest value of `i` due to the block-scoped `let` declaration. The `var` declaration would have resulted in all closures sharing the same `i`, leading to all functions logging `3`.
Frequently asked questions
- How many questions are in the Intermediate JavaScript test?
- 30 questions (18 multiple choice, 9 multi-answer, 3 predict-the-output), with 35 minutes to answer them.
- What does the Intermediate JavaScript test cover?
- Closures and scope, this and prototypes, Event loop and microtasks, Promises and async/await, ES modules.
- Is the Intermediate JavaScript test free?
- Yes. It is free to take with a readyforhires account.
- What level is the Intermediate JavaScript test?
- Intermediate, in Frontend.