14. Closures

Que.1

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

Output: Click here

Explaination:     The code calls the returned function four times using the `alert` function to display the result of each call. The expected output is a series of alerts showing the numbers 1, 2, 3, and 4, each on a separate line.

Que.2

  let count = 0;
  (function () {
    if (count === 0) {
      let count = 1;
      console.log(count); // What is logged?
    }
    console.log(count); // What is logged?
  })();
        

Output Link: Click here

Output:   1
0

Que.3

  for (var i = 0; i < 3; i++) {
    setTimeout(function log() {
      console.log(i); // What is logged?
    }, 1000);
  }       
        

Output Link: Click here

Output:   3
3
3

Explaination:     1. The for loop runs three times, with i starting at 0 and incrementing by 1 each time.

2. For each iteration of the loop, a setTimeout function is called. This function sets a timer to execute a callback function (in this case, log) after a specified delay (in this case, 1000 milliseconds or 1 second).

3. When the timer expires, the callback function is added to the event queue to be executed by the JavaScript runtime when it is available.

4. However, before the callback function is executed, the for loop has completed its iterations, and i has been left with the value of 3.

5. Therefore, when each callback function is executed, it logs the current value of i, which is 3.

Que.4 angle using inner function. In this case outer function should accept parameter length and inner function should accept parameter breadth.

  function calculateAngle(length) {
    return function(breadth) {
      const radian = Math.atan2(breadth, length);
      const angle = radian * (180 / Math.PI);
      return angle;
    }
  }
  const angleFunction = calculateAngle(3); // pass length as parameter
  const angle = angleFunction(4); // pass breadth as parameter
  console.log(angle); // Output: 53.13010235415598
      

Output Link: Click here

Output:   53.13010235415598

Que.5 Take a variable in outer function and create an inner function to increase the counter every time it is called

  function outer() {
    let count = 0;
  
    function inner() {
      count++;
      console.log(`Count is now ${count}`);
    }
  
    return inner;
  }
  
  // Create a new function that has access to the count variable
  const incrementCount = outer();
  
  // Call the inner function multiple times to see the count increase
  incrementCount(); // Count is now 1
  incrementCount(); // Count is now 2
  incrementCount(); // Count is now 3
      

Output Link: Click here

Output:   Count is now 1
Count is now 2
Count is now 3

Que.6 Print Output

  var a = 12;
  (function () {
    alert(a);
  })(); 
        

Output Link: Click here

Output:   12

Que.7

  var a = 10;
  var x = (function () {
    var a = 12;
    return function () {
      alert(a);
    };
  })();
  x();
        

Output Link: Click here

Output:   12

Que.8

  var globalVar = "xyz";
  (function outerFunc(outerArg) {
      var outerVar = 'a';
      
      (function innerFunc(innerArg) {
      var innerVar = 'b';
      
      console.log(
          "outerArg = " + outerArg + "\n" +
          "innerArg = " + innerArg + "\n" +
          "outerVar = " + outerVar + "\n" +
          "innerVar = " + innerVar + "\n" +
          "globalVar = " + globalVar);           
      })(456);
  })(123);
    

Output Link: Click here

Output:   outerArg = 123
innerArg = 456
outerVar = a
innerVar = b
globalVar = xyz

Back
Next