2.Difference between undefined and null data type, different ways to get undefined?
If a value of a variable is either not declared or initialized, then the value will be undefined. But if we set a value of a variable to null intentionally only then its value will be null. Null means intentionally missing value.
we can get the value of a variable undefined in many ways.
· if we don’t initialize a value in the first place.
· If it’s a function but we don’t return anything from that function.
· if it’s a function but when calling that function we don’t pass any required parameter.
· if it's an object but the property we want does not exist in that object.
· if it's an array but the element we want does not belong to that array.
· if we explicitly initialize a value of undefined.Though it's not recommended.
3. what are double equal (==) vs triple equal (===), implicit conversion?
When checking any condition if we put double equal between two compared values, it will return true if both variables have the same values it will not check the data type.
But when checking the value of two variables, if we put triple equal (===) between them it will check both value and data type.
4.Define Scope and block scope.
When we declare a variable using var inside a scope, it gets hoisted on the top of that scope. Its means it creates a global scope and it will be available from within and outside that function. But the variable value won’t get hoisted only the declaration part does.
When we declare a variable using let or const, it creates a scope level variable. That variable will only be available within that scope. It won’t get hoisted on the top of that scope.
5.Define Closure, encapsulation, and private variable.
When a function is called or returned from or inside a parent function, then the child function creates an enclosed/encapsulated environment and if he uses any variable of parent function, it will keep a reference of that variable.
6. Difference between bind, call and apply.
If we want to use a method of an object with another object that does not have that method inside itself, we can initially use the bind, call or apply method.
Bind: Bind will only bind the method to the new object but won’t change or affect its original parent object.
Call: Call is also used to use a method of one object with another object. It doesn’t require assigning the call method to any variable, we can call it directly by passing the parameters separated by a comma.
Apply: Apply is quite similar to the call method. The only difference is that when calling the method we have to pass the required in an array.
7. Define window, global variable, global scope.
Variable accessibility is determined by scopes. Scope is created with the creation of function. Variable declared within one function will have local scope within that function, which means it won’t be available from outside that function.
If a variable is not defined within any function it creates a global scope, which means it will be accessible from any function of that program.
If we don’t declare a variable while initializing a value inside a variable, it will also create a global scope.
8. How to understand this keyword?
this alone refers to the global object. In a function, this refers to the global object.
When working with any event this refers to the environment of that event. In call or apply method this refers to the object it is called with. In strict mode, this means undefined.
In this example, setTimeout function will be executed after all the codes are executed and at least after 5 seconds.
setInterval functions set a function to be executed after a specific time interval. It takes two arguments which are callback function and interval time.
In this example all these functions on call stack like this-
the first( ) will be executed,then the second( ) and finally third( ) will be executed.
In this example third( ) and second( ) will go to the stack first.but setTimeout function will go to the message queue. After the call stack will be clear the setTimeout function will be thrown from message queue to stack and it will be executed.
Heap represents a large region where the memory allocation of objects happens.