Giter VIP home page Giter VIP logo

javascript.interview's Introduction

Javascript Interview

This documents contains the most actual and important questions for Javascript developer position. It will helps you to compose question list for your own interview or prepare to inverview.

Table of contents:

Inheritance in Javascript

Question i1: Write Object.create polyfill

For implementing use this kata please.

Question i2: What is functional inherence pattern? How to create protected methods and private propreties?

Question i3: What will output the following code and why:

var baseObject = {
  prop: "Base value",
  getProp: function() {
    return this.prop;
  }
};

var otherObject = Object.create(baseObject);

otherObject.prop = "Other value";

console.log(otherObject.getProp());
delete otherObject.prop;
console.log(otherObject.getProp()); 

Answer

Console will output

Other value
Base value

Closure in Javascript

Question c1: What is Closure in Javascript?

MDN definition: Closures are functions that refer to independent (free) variables (variables that are used locally, but defined in an enclosing scope). In other words, these functions 'remember' the environment in which they were created.

Javascript.isSexy: A closure is an function that has access to the outer (enclosing) function’s variables-scope chain.

Question c2: What will output the following code and why:

function outerFunction() {
    var flag = undefined;

    function innerFunction() {
      if (true) {
        flag = true;
      } else {
        var flag = false;
      }

      console.log(flag);
    }

    console.log(flag);
    innerFunction();
}

outerFunction();

Answer

Console will output the followings:

undefined
true

Question c3: Resolving example of problem code

What will output these example?

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

How to fix it to output numbers from 0 to 9?

Answer

Ther're several ways to resolve code above.

  1. By creating a IIFE (Immediately Invoked Function Expression):
for (var i=0; i < 10; i++){
    setTimeout((function(param){
        console.log(i);
    })(i), 1000);
}
  1. By using ES6 feature, by using let syntax particularly:
for (let i=0; i < 10; i++){
    setTimeout(function(){
        console.log(i);
    }, 1000);
}
  1. By binding console.log function:
for (var i=0; i < 10; i++){
    setTimeout(console.log.bind(console, i), 1000);
}

Question c4: Write nextID function that will generate ID incrementally after each calling

There's an example of function output:

nextID(); // output: 1
nextID(); // output: 2
nextID(); // output: 3

Answer

We should use closure in IIFE:

var nextID = (function() {
   var id = 0;
   return function() {
   		console.log(++id);
   }
})();

Bind, apply and call function methods

Question b1: Write sum function

This function must meet conditions:

typeof sum(1) === 'function'
typeof sum(1)(2) === 'function'
typeof sum(1)(2)() === 'number'

sum(1)() === 1
sum(1,2)(3)() == 6
sum(1,2)(3,4)() === 10

Answer

function sum (fn) {
  var sumArguments = Array.prototype.slice.call(arguments);
    
  return function sumInner() {
      if (arguments.length) {
          sumArguments = sumArguments.concat(Array.prototype.slice.call(arguments));

          return sumInner;
      } else {
          return sumArguments.reduce(function(total, arg) {
            return total + arg;
          }, 0);
      }
  };
}

Hoisting in Javascript

Question h1: Explain what is hoisting in Javascript

Answer

W3School Hoisting is JavaScript's default behavior of moving declarations to the top. In JavaScript, a variable can be declared after it has been used. In other words; a variable can be used before it has been declared.

Question h2: What will output the following code and why:

var a = 1; 
function bar() { 
    if (!a) { 
        var a = 10; 
    } 
    console.log(a); 
} 
bar();

Answer

Console will output :

10

Question h3: What will output the following code and why:

var a = 1;
function b() {
    a = 10;
    return;
    function a() {}
} 
b(); 
console.log(a);

Answer

Console will output :

1

Contributing

I would be thankful for your issues and pull requests

javascript.interview's People

Contributors

ufocoder avatar

Watchers

 avatar

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.