Search This Blog

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

No comments:

Post a Comment