• Tutorials
  • Courses
  • Tracks

JavaScript Functions

Last Updated :
Discuss
Comments

Question 1

What is the right output for the code below?

let count = 0;

const inc = () => ++count;

[inc(), inc(), inc()];

console.log(count);


 

  • 0

  • 1

  • 3

  • undefined

Question 2

How do you define a function in JavaScript?

  • function myFunc() {}

  • def myFunc() {}

  • function = myFunc {}

  • func myFunc() {}

Question 3

What will be the output of the following code?

JavaScript
const arr = [1, 2, 3, 4, 5, 6];
const result = arr.filter(num => num % 2 === 0)
                  .map(num => num * 2)
                  .reduce((acc, curr) => acc + curr, 0);
console.log(result);
  • 24

  • 22

  • 12

  • 18

Question 4

What will be the output when factorial(7) is called?

JavaScript
function factorial(n) {
  if (n === 0 || n === 1) return 1;

  let result = 1;
  function recursiveFactorial(n) {
    if (n === 2) return;
    result *= n;
    recursiveFactorial(n - 1);
  }

  recursiveFactorial(n);
  return result;
}

console.log(factorial(7));


  • 5040

  • 2520

  • 720

  • 50400

Question 5

What will typeof null return?


  • null

  • undefined

  • object

  • function

Question 6

What happens here?

const x = () => {};

console.log(x.name);


  • ""

  • undefined

  • "x"

  • Error

Question 7

function foo(x, y, z) {

return x + y + z;

}

const bar = foo.bind(null, 1);

console.log(bar(2, 3));


  • NaN

  • 123

  • 6

  • undefined

Question 8

Consider the following code implementation for calculating the factorial of a number using memoization:

JavaScript
function factorial(n, memo = {}) {
  if (n <= 1) return 1;
  if (memo[n]) return memo[n];
  memo[n] = n * factorial(n - 1, memo);
  return memo[n];
}

What would happen if you call the function factorial(5) twice without modifying the memo object between the calls?

  • The second call to factorial(5) will compute all the values again.

  • The second call to factorial(5) will return the result immediately from the memo object without recomputing.

  • The second call to factorial(5) will throw an error due to an invalid memoization structure.

  • The second call to factorial(5) will return undefined.

Question 9

What is the output?

(function f(){

console.log(typeof f);

})();


  • object

  • undefined

  • function

  • string

Question 10

What will the following code output?

JavaScript
const add = (a, b) => a + b;
console.log(add(5));


  • 5

  • NaN

  • Error

  • undefined

There are 10 questions to complete.

Take a part in the ongoing discussion