GithubHelp home page GithubHelp logo

javascript-interview-questions's Introduction

Common JavaScript Interview Questions and Answers

Q1. Write a method sum that would work with the following syntax?

console.log(sum(2,3));   // Outputs 5
console.log(sum(2)(3));  // Outputs 5
Solution
function sum(x) {
  if (arguments.length == 2) {
    return arguments[0] + arguments[1];
  } else {
    return function(y) { return x + y; };
  }
}

Q2. What gets logged in the following code?

(function() {
    console.log(1); 
    setTimeout(function(){console.log(2)}, 1000); 
    setTimeout(function(){console.log(3)}, 0); 
    console.log(4);
})();
Solution
1
4
3
2

Q3. What will be the output? How can we fix this?

for (var i = 0; i < 5; i++) {
	setTimeout(function() { console.log(i); }, i * 1000 );
}
Solution

Output:

5
5
5
5
5

The fix:

  1. Using let
for (let i = 0; i < 5; i++) {
	setTimeout(function() { console.log(i); }, i * 1000 );
}
  1. Using closures:
for (let i = 0; i < 5; i++) {
	setTimeout(function() { console.log(i); }, i * 1000 );
}

Q4. What is the output?

var x = 21;
var foo = function () {
    console.log(x);
    var x = 20;
};
foo();
Solution
undefined

Q5: What will be the output?

console.log(1 < 2 < 3);
console.log(3 > 2 > 1);
Solution
true
false

Q6: What will be the output?

var b = 1;
function outer(){
   	var b = 2
    function inner(){
        b++;
        var b = 3;
        console.log(b)
    }
    inner();
}
outer();
Solution
3

Q7. Memoize a function.

Solution
function memoize(fn) {
  const cache = {}
  return function() {
    const args = JSON.stringify(arguments);
    if (cache[args]) {
      return cache[args]
    }
    const evaluatedValue = fn.apply(this, arguments);
    cache[args] = evaluatedValue;
    return evaluatedValue;
  }
}

// example use
function factorial(n) {
   if(n === 0 || n === 1) {
     return 1
   }
   return factorial(n-1) * n; 
}
const memoizedFactorial = memoize(factorial)
factorial(1000) // slow
memoizedFactorial(1000) // faster

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.