Search This Blog

Q6-Q10

Q6. How functions are different in JavaScript?
Q7. What are drawback of closure?
Q8. What is the type of array and How to check if object is array type or not in JavaScript?
Q9. What is the use of .join in JavaScript?
Q10. What is event loop in JavaScript?
------------------------------------------------------------------------------------------------------------------------
Q6. How functions are different in JavaScript?

Answer:
  • In JavaScript, functions are first-class objects; means they are type Object. 
  • In JavaScript they can be “stored in variables". 
  • They can be passed as arguments to another functions,
  • They can be created within functions, and 
  • They can be returned from others functions.
------------------------------------------------------------------------------------------------------------------------
Q7. What are drawback of closure?

Answer:
As long as closures are active, this memory cannot be garbage collected. Therefore, closures can lead to memory leaks if not used well.
------------------------------------------------------------------------------------------------------------------------
Q8. What is the type of array and How to check if object is array type or not in JavaScript?

Answer:
Type of array is object in JavaScript.
The Array.isArray() method determines whether an object is an array.

(function(
var obj1 = ['a','b','c'];
var obj2 = new String('String');

console.log(Array.isArrary(obj1)); //true
console.log(Array.isArrary(obj2)); //false
))()

------------------------------------------------------------------------------------------------------------------------
Q9. What is the use of .join in JavaScript?

Answer:
it joins the element of array in to comma separated string.

example.
var fruits = ['apple','banana','carrot']
var strFruits = fruits.join();
console.log(strFruits); // output would be -- "apple,banana,carrot"

------------------------------------------------------------------------------------------------------------------------
Q10. What is event loop in JavaScript?

Answer:

Every JavaScript call work mainly around 4 things 1) call stack, 2) Event table 3) Event queue and 4) Event loop.

  • Call stack- JavaScript call stack keeps the track of current function executing and what to execute after that.
  • Event table - Every time you call a setTimeout function or you do some async operation — it is added to the Event Table.
  • Event Queue - When function get the result it put that in Event Queue.
  • Event loop - Finally Event loop is a constantly running process that checks if call stack is empty then it look at event queue. If it found something in event queue it moves it to call stack.

https://hackernoon.com/understanding-js-the-event-loop-959beae3ac40
------------------------------------------------------------------------------------------------------------------------

Q1-Q5

Q1. What is callback (not closure) in JavaScript?
Q2. What is syntax of callback in JavaScript?
Q3. What is callback hell?
Q4. object and variables are copied by value or by reference in JavaScript.
Q5. What is the difference between shallow copy and deep copy in JavaScript
--------------------------------------------------------------------------------------------------------------------------
Q1. What is callback (not closure) in JavaScript?

Answer:

In JavaScript, functions are first-class objects; that is, functions are of the type Object and they can be used in a first-class manner like any other object (String, Array, Number, etc.) since they are in fact objects themselves. They can be “stored in variables, passed as arguments to functions, created within functions, and returned from functions”


They are also known as Higher-order functions. Call back function is a function that is passed to another function as a parameter, and the callback function is called (or executed) inside the that another function. in simple terms A callback is a function to be executed after another function is executed

A callback is a function to be executed after another function is executed. 
http://javascriptissexy.com/understand-javascript-callback-functions-and-use-them/

--------------------------------------------------------------------------------------------------------------------------
Q2. What is syntax of callback in JavaScript?

Answer:


(function(){
  console.clear();

  function do_a(callbackFunction){
    setTimeout(function(){
      console.log('do_a');

      //if callback exist, execute it
      callbackFunction && callbackFunction();
    }, 1000);
  }

  function do_b(){
    console.log('do_b');
  }

  do_a(function(){
    do_b();
  });

})();


In above example we are passing function as a parameter to function do_a();
This will ensure that do_b() will be called after completion of do_a(). We are also checking if callback function exits or not.

--------------------------------------------------------------------------------------------------------------------------
Q 3. What is callback hell?

Answer: 
Its a anti pattern seen in code written by developers when handling many async programing. There could be the case of handling "on success" or "on failure". Its NOT a recommended way of coding.

(function(){
  console.clear();

function callBackHellProblem() {
  setTimeout(function() {
    console.log('1. First thing setting up second thing');
    setTimeout(function() {
      console.log('2. Second thing setting up third thing');
      setTimeout(function() {
        console.log('3. Third thing setting up fourth thing');
        setTimeout(function() {
          console.log('4. Fourth thing');
        }, 1000);
      }, 1000);
    }, 1000);
  }, 1000);
};

  callBackHellProblem();
})();


--------------------------------------------------------------------------------------------------------------------------
Q4. object and variables are copied by value or by reference in JavaScript.

Answer:
  • In general variables(primitive datatypes variables) are copied by value and objects are copied by reference in JavaScript.
  • Arrays also copied by reference in javascript like objects. 
  • Shallow copy and deep copy is a way in which we can make a "copy be value" of objects and arrays which by default "copy by reference"

example of objects
(function(){
  var obja = {first:1};
  var objb = obja; // copied by reference

  objb.first = 2;
  console.log(obja.first); ///2
})()

example of variables
(function(){
 var varA = 1;
  var varB = varA;
  varB = 2;
  console.log(varA); //1
})();
--------------------------------------------------------------------------------------------------------------------------
Q5. What is the difference between shallow copy and deep copy in JavaScript

Answer:

Definition:
Shallow copy: "Copy by value" top level elements and "copy by reference" nested elements. 

Deep Copy: "Copy by value" top level elements as well as nested elements. Deep copy is a way by which we can copy array or object by value as by default array or object are copied by reference in javascript. 
JSON.parse(JSON.stringify(obj)) can be used to make deep copy.

https://www.youtube.com/watch?v=mNYo7LTuOyM