Skip to content

Instantly share code, notes, and snippets.

@josephwambura
Last active November 9, 2022 10:25
Show Gist options
  • Save josephwambura/da065acda05654eb03964ea549c6a5e2 to your computer and use it in GitHub Desktop.
Save josephwambura/da065acda05654eb03964ea549c6a5e2 to your computer and use it in GitHub Desktop.
Javascript 101

JavaScript 101 QAs

  1. What method returns a promise after a group of promises have either been fulfilled or rejected?

     Promise.allSettled()
    
  2. Which function correctly demonstrates the use of the rest parameter (foo), allowing the function to accept an infinite number of arguments, beyond the required parameters a and b?

     function bar(a, b, ...foo) {
      foo.map(() => ())
     }
    
  3. You have 20 promises in your script. You must return the value of only the fastest promise. The promises can either be fulfilled or rejected. Which promise method is useful in this situation?

     race
    
  4. What would you use to enclose multi-line strings in JavaScript?

     Backticks
    
  5. If a promise statement contains multiple callbacks using .then() statements, in what order are the callbacks executed?

     The order they are written in the statement
    
  6. What type of method is used in promise chaining?

     .then()
    
  7. Which keyword can you use to declare a block-scoped, mutable variable?

     let
    
  8. After the following code executes, what is printed to the console?

     const iterable = [10];
    
     for (let value of iterable) {
         console.log(value);
     }
    

The output is

    10
  1. After the following code is executed, what is printed to the console?

     var array = ['a', 'b'];
     var elements = [0, 1, 2];
     array.push.apply(array, elements);
     console.log(array);
    

The output is

    ["a", "b", 0, 1, 2]
  1. Given a class Sheep inheriting from a class Animal, what must you do before using this in Sheep's constructor?

    Use the super keyword to call the constructor of the parent class
    
  2. How would you re-export an imported member (foo) into a parent module from a module (bar.js)?

    export foo from 'bar.js';
    
  3. What is the scope of a value declared with const?

    the block in which it is declared.
    
  4. You are thinking of using a third-party application to help build a new piece of secure software for your company. What must you do to first check for potential threats of using that third-party application?

    Look at the third-party application’s Common Vulnerabilities and Exposures (CVEs).
    
  5. The following operation converts an iterable object to an array:

    const s = new Set(['Apple', 'Mango', 'Kiwi', 'Kiwi', 'Grapes']);
    
    console.log(Array.from(s));
    

The resulting array consists of only four elements instead of five. Why?

    The set s stores only unique values. As a result, it discards the duplicate element.
  1. What is the value of found in the following code snippet?

    const array1 = [6, 12, 10, 130, 44];
    const found = array1.findIndex(element => element > 10);
    

The output is

    1
  1. What is the correct syntax for declaring a default parameter?

    function square(n = true) { 
     ...
    }
    
  2. You are using reflect objects to simplify the creation of your proxy. How could you check if a specific property exists in an object?

    Reflect.has()
    
  3. Which is a correct method of importing all exports?

    import * as myModule from 'my-module.js';
    
  4. What is the value of foo printed to the console from the following code snippet?

    let a = [1,2,3];
    let foo = Array.from(a);
    a = [2,4,6];
    console.log(foo);
    

The output is

    [1,2,3]
  1. What is the correct syntax for declaring an object literal?

    const obj = {};
    
  2. You created a new promise function that returns a rejected status every time the promise function is executed. Given the following code, why are the statements after the reject() function not executed?

    let p = new Promise(function(resolve, reject) {
      resolve("successful!");
      reject(new Error(" This parameter is invalid"));
      let isError = "1";
    });
    

It is because,

    Only one instance of resolve or reject can be called by the code. Anything after is ignored by the promise function.
  1. What would you call to create an array from the following Map object?

    let myMap = new Map();
    myMap.set('item1','Foo');
    myMap.set('item2','Bar');
    

I would use:

    Array.from(myMap.entries());
  1. After the following code is executed, what is printed to the console? console.log(eval('2 + 2'));

    4
    
  2. Which code snippet correctly shows unpacking the arguments object into an array?

    Array.from(arguments);
    
  3. What boolean is returned from the following code? Symbol("foo") !== Symbol("foo")

    true
    
  4. The following four promises are nested in another promise:

    const p1 = new Promise((resolve, reject) => 
                           setTimeout(reject, 2000, 'Foo'));
    const p2 = new Promise((resolve, reject) => 
                           setTimeout(reject, 1040, 'Bar'));
    const p3 = new Promise((resolve, reject) => 
                           setTimeout(reject, 500, 'Que'));
    const p4 = new Promise((resolve, reject) => 
                           setTimeout(reject, 40, 'Quaz'));
    const p = [p1, p2, p3, p4];
    
    Promise.race(p)
      .then(value => console.log(value))
      .catch(err => console.log(err));
    

The code outputs Quaz. You replace Promise.race() with Promise.any() and you receive no value as an output. Why?

It's because,

    Promise.any() always needs at least one promise to be successful which is not the case with Promise.race(). As a result, no value is returned.
  1. Which statement is true regarding an arrow function?

    You can use it as an anonymous function.
    
  2. You created a promise for an asynchronous operation. Which handler will be called whether the promise succeeds or fails?

    finally
    
  3. What primitive type is the variable result? const result = NaN

    Number
    
  4. What is the value of "x" after the following snippet executes?

    var x, y = 42;
    

The value of x is

    undefined
  1. What object is used to access globally scoped variables in a browser for a front-end web application?

    window
    
  2. How could you make the following instance into an object literal?

    const foo = function(name) {
      this.name = name;
      this.greet = function(greeting) {
        console.log(`${greeting}, ${this.name}`);
      }
    }
    

Solution:

    const foo = {
      name: "User1",
      greet: function(greeting, name) {
        console.log(`${greeting}, ${name}`);
      } 
    }
  1. You created a new variable object named house from the class named building. The building class contains getter and setter functions for a variable named type. How can you make house object's type value equal to "Residential"?

    house.type.set("Residential");
    
  2. What will the following code snippet return?

    function square(n) { 
      n * n; 
    }
    square(5);
    

Output:

   undefined
  1. What is an example of Prototype Pollution?

    Attacking the object prototype by adding, modifying, or deleting its properties
    
  2. How would you refactor the following greeting object to make the fullname property private?

    var greeting = {
        fullname: "Person One",
        greet: (message, name) => {
            console.log(message + " " + name + "!!");
        }
    };
    

Solution

    Move fullname inside the greet function as a function scoped variable.
  1. What will the following code, using the arguments object, evaluate to?

    function foo(n) { 
      return arguments[0] * arguments[0]; 
    }
    foo(5);
    

Output

    25
  1. You come across the following line of code that is linked to sensitive data. What security issue is present?

    exampleName[userInput[1]] = userInput[2]
    

The security issue present is:

    Bracket object notation from the user input would allow the code to be edited.
  1. You created a static method Calories within the Fruits class. An instance of this class is stored in a variable obj as follows:

    const obj = new Fruits();
    

You call the Calories method using the syntax obj.Calories();, but it results in an error suggesting that obj.Calories is not a function. What should you use to fix the code?

I would use:

    Fruits.Calories();
  1. You frequently use third-party libraries in your JavaScript code. What is a way that an attacker could cause Prototype Pollution?

    The attacker could provide JSON data in the third-party code that has the __proto__ property.
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment