readyforhires

JavaScript test (Beginner)

25 questions · 30 minutes · Free

Closures, scope, prototypes and the async model.

Take the JavaScript test

What it covers

Interview format

Earn a certificate

The 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 learning path certificate for Frontend Developer, made out to “Your name” and marked SAMPLE
Pass with 70% or more to earn a verifiable certificate. This is a sample: yours carries your name, the date you earned it and its own verify code.

Sample questions

5 sample questions from the JavaScript test, with the answers. Samples are taken out of the live test, so you will not meet these when you sit it.

  1. What will the following function log to the console when called without any arguments? function logGreeting(greeting = 'Hello, World!') { console.log(greeting); }

    1. The function will log 'undefined' to the console.
    2. The function will not execute because the 'greeting' parameter is not provided.
    3. The function will log 'Hello, World!' to the console.
    4. The function will throw an error because the 'greeting' parameter is not provided.

    Answer: C. The function will log 'Hello, World!' to the console.

    In JavaScript, if a function parameter is not provided when the function is called, the default value specified in the function declaration is used. In this case, the default value for 'greeting' is 'Hello, World!', so the function will log that value to the console.

  2. What is the output of the following code snippet? Consider the order of execution in the event loop and the microtask queue. ```js console.log(1); setTimeout(() => console.log(2), 0); Promise.resolve().then(() => console.log(3)); new Promise(r => { console.log(4); r(); }); console.log(5); ```

    1. The code will print: 1 4 5 3 2
    2. The code will print: 1 5 4 3 2
    3. The code will print: 1 2 3 4 5
    4. The code will print: 1 2 4 5 3

    Answer: A. The code will print: 1 4 5 3 2

    The microtask queue is processed before macrotasks. The `Promise.resolve().then(...)` and `new Promise(r => { ... })` are microtasks and are executed before `setTimeout(...)` which is a macrotask. Thus, the order of execution is 1 (immediate), 4 (from the promise executor), 5 (immediate), 3 (microtask), and 2 (macrotask).

  3. A developer is encountering an issue where a for...in loop is iterating over properties from the prototype chain, leading to unexpected results. What should they do to fix this issue?

    1. Use Object.keys() instead of for...in to get only the object's own enumerable properties.
    2. Add a nonenumerable property to the object using Object.defineProperty with enumerable: false.
    3. Use Object.getOwnPropertyNames() to get all properties, including nonenumerable ones.
    4. Use a for loop with Object.keys() to iterate over the object's properties.

    Answer: A. Use Object.keys() instead of for...in to get only the object's own enumerable properties.

    The for...in loop iterates over all enumerable properties of an object, including those inherited from the prototype chain. This can lead to unintended behavior if the object has inherited properties. Using Object.keys() ensures that only the object's own enumerable properties are included, avoiding issues with prototype chain enumeration.

  4. Which of the following correctly describes the usage of `apply` and `call` when dynamically binding `this`?

    1. Use `apply` when you want to dynamically bind `this` to an object and pass an array of arguments; use `call` when you want to pass arguments individually.
    2. Use `apply` when you want to pass arguments individually; use `call` when you want to pass an array of arguments.
    3. Use `apply` when you want to bind `this` to a specific object; use `call` when you want to bind `this` to the global object.
    4. Use `apply` when you want to bind `this` permanently; use `call` when you want to bind `this` temporarily.

    Answer: A. Use `apply` when you want to dynamically bind `this` to an object and pass an array of arguments; use `call` when you want to pass arguments individually.

    The `apply` method is used to call a function with a given `this` value and an array of arguments. On the other hand, `call` is used to call a function with a given `this` value and individual arguments. Therefore, `apply` is more suitable for dynamically binding `this` when passing an array of arguments, while `call` is appropriate for passing arguments individually.

  5. Which of the following accurately describes the difference between using a closure and a module pattern in terms of memory management?

    1. Using a closure to store data in a function and not releasing the reference to it can cause a memory leak, whereas using a module pattern with a private variable can avoid this by encapsulating the data and allowing it to be garbage collected when no longer needed.
    2. Using a closure is always better than a module pattern for managing memory, as closures are more efficient in garbage collection and do not require encapsulation.
    3. Using a closure and a module pattern are equally effective in preventing memory leaks, as both encapsulate data and manage references properly.
    4. Using a module pattern can cause a memory leak because it exposes private variables, whereas using a closure avoids this by keeping data hidden and not retaining unnecessary references.

    Answer: A. Using a closure to store data in a function and not releasing the reference to it can cause a memory leak, whereas using a module pattern with a private variable can avoid this by encapsulating the data and allowing it to be garbage collected when no longer needed.

    A memory leak can occur when a closure retains references to objects that are no longer needed, preventing the garbage collector from reclaiming the memory. The module pattern, on the other hand, encapsulates private variables and avoids unnecessary references, allowing for proper memory management.

Frequently asked questions

How many questions are in the JavaScript test?
25 questions (15 multiple choice, 8 multi-answer, 2 predict-the-output), with 30 minutes to answer them.
What does the JavaScript test cover?
Scope and closures, this binding, Prototypes, Promises and microtasks, Equality and coercion.
Is the JavaScript test free?
Yes. It is free to take with a readyforhires account.
What level is the JavaScript test?
Beginner, in Frontend.

More Frontend tests

Roles that test these skills

Companies that test these skills

All practice tests · All Frontend tests

Last updated