Skip to content

Instantly share code, notes, and snippets.

@hafizali05
Last active November 30, 2018 06:26
Show Gist options
  • Save hafizali05/43c0734acc5cf512449409936140ed20 to your computer and use it in GitHub Desktop.
Save hafizali05/43c0734acc5cf512449409936140ed20 to your computer and use it in GitHub Desktop.

#JS Interview Questions

JAVASCRIPT PROMISE

The Promise object represents the eventual completion (or failure) of an asynchronous operation, and its resulting value.

Closure

A closure is a function having access to the parent scope, even after the parent function has closed.


var add = (function () {
    var counter = 0;
    return function () {counter += 1; return counter}
})();

add();
add();
add();

Example Explained

The variable add is assigned the return value of a self-invoking function. The self-invoking function only runs once. It sets the counter to zero (0), and returns a function expression. This way add becomes a function. The “wonderful” part is that it can access the counter in the parent scope. This is called a JavaScript closure. It makes it possible for a function to have “private” variables. The counter is protected by the scope of the anonymous function, and can only be changed using the add function.

Scope

Scope determines the accessibility of variables, objects, and functions from different parts of the code.

In JavaScript there are two types of scope:

  • Local scope
  • Global scope JavaScript has function scope: Each function creates a new scope. Scope determines the accessibility (visibility) of these variables. Variables defined inside a function are not accessible (visible) from outside the function.

If you assign a value to a variable that has not been declared, it will automatically become a GLOBAL variable.

This keyword

In a function definition, this “refers” to the owner of the function

var *person* = {
    firstName: “John”,
    lastName : “Doe”,
    id       : 5566,
    fullName : function() {
        return this.firstName + “ “ + this.lastName;
    }
};

In the example above, this refers to the person object. The person object “owns” the fullName method.

Call

  1. Its a type of method re-use, where we can write methods which can be used on different objects.
  2. With call method, an object can use methods belonging to another object.
  3. Or
  4. A method in another object can be be applied on the current object
    1. This example calls the fullName method of person, using it on person1:

Example

var person = {
    *fullName*: function() {
        return this.firstName + “ “ + this.lastName;
    }
}
var person1 = {
    firstName:”John”,
    lastName: “Doe”,
}
var person2 = {
    firstName:”Mary”,
    lastName: “Doe”,
}
person.fullName.call(*person1*);  // Will return “John Doe”

Apply

  1. Its a type of method re-use
  2. The difference between call and apply is, you can use arrays as argument instead of just an object as an argument

var person = {     fullName: function(city, country) {         return this.firstName + “ “ + this.lastName + “,” + city + “,” + country;     } } var person1 = {     firstName:”John”,     lastName: “Doe”, } person.fullName.call(person1, “Oslo”, “Norway”);

Bind

Bind () Allows us to Borrow Methods
        // Here we have a cars object that does not have a method to print its data to the console
        var cars = {
            data:[
                {name:”Honda Accord”, age:14},
                {name:”Tesla Model S”, age:2}
            ]

        }

        // We can borrow the showData () method from the user object we defined in the last example.
        // Here we bind the user.showData method to the cars object we just created.
        cars.showData = user.showData.bind (cars);
        cars.showData (); // Honda Accord 14

Pure functions

A pure function doesn’t depend on and doesn’t modify the states of variables out of its scope. Concretely, that means a pure function always returns the same result given same parameters. Its execution doesn’t depend on the state of the system. http://www.nicoespeon.com/en/2015/01/pure-functions-javascript/

Constructor

The constructor method is a special method for creating and initializing an object created within a class.

JavaScript Hoisting

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.

What is the difference between encryption and hashing? Encryption is two way Hashing is one way, can’t be undone https://github.com/ajzawawi/js-interview-prep/blob/master/answers/general/encryption-vs-hashing.md

  1. Polymorphism

  2. Inheritance

  3. JS classes

  4. CORS

  5. Public and private methods in JS

  6. Javascript prototypes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment