#JS Interview Questions
The Promise object represents the eventual completion (or failure) of an asynchronous operation, and its resulting value.
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();
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 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.
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.
- Its a type of method re-use, where we can write methods which can be used on different objects.
- With call method, an object can use methods belonging to another object.
- Or
- A method in another object can be be applied on the current object
-
- This example calls the fullName method of person, using it on person1:
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”
- Its a type of method re-use
- 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 () 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
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/
The constructor method is a special method for creating and initializing an object created within a class.
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
-
Polymorphism
-
Inheritance
-
JS classes
-
CORS
-
Public and private methods in JS
-
Javascript prototypes