Search This Blog

Q21-Q25

Q21. What is LET keyword in JavaScript?
Q22. What are closures in JavaScript?
Q23. What is variable Hoisting in JavaScript?
Q24. What is event propagation in JavaScript?
Q25. Is JavaScript single threaded or multi threaded?
------------------------------------------------------------------------------------------------------------------------
Q21. What is LET keyword in JavaScript?

Answer:

LET allows you to declare variables that are limited in scope to the block.it is a block scope variable. This is unlike the var keyword, which defines a variable globally, or locally to an entire function regardless of block scope. ie Var keyword is functional scope in general but if var keyword is not defined inside a function it has global scope. 

--- Problem with VAR--
(function(){
  console.clear();
var x = 1;
if (true) {
  var x = 2;
  console.log(x); //output 2
}
console.log(x); //output 2
})();

-- Use of LET keyword --
(function(){
  console.clear();
let x = 1;
if (true) {
  let x = 2;
  console.log(x); //output 2
}
console.log(x); //output 1
})();

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let
------------------------------------------------------------------------------------------------------------------------
Q22. What are closures(not callback) in JavaScript?

Answer: 

A closure is an inner function that has access to the outer (enclosing) function's variables—scope chain. The closure has three scope chains: it has access to its own scope (variables defined between its curly brackets), it has access to the outer function's variables, and it has access to the global variables.

Although function can access variable from outside in concept of closure function still individuality of every call is maintained
eg.

function makeCounter() {
  let count = 0;
  return function() {
    return count++;
  };
}

let counter1 = makeCounter();
let counter2 = makeCounter();

alert( counter1() ); // 0
alert( counter1() ); // 1

alert( counter2() ); // 0 (independent)


https://javascript.info/closure
http://javascriptissexy.com/understand-javascript-closures-with-ease/
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures
------------------------------------------------------------------------------------------------------------------------
Q23. What is variable Hoisting in JavaScript?

Answer: 

Hoisting is JavaScript's default behavior of moving declarations to the top. In JavaScript, a variable can be declared after it has been used. 
Its important to note JavaScript only hoists declarations, not initialization.
 means if you can give some value to variable x first and write var x below it - No Error

But if you write alert (x + 5); and below it you write var x = 2; this wont work and will give you error.

Similarly below will not work
console.log(name); // Uncaught ReferenceError: name is not defined
let name = "Andrew";
because the name value is giving in next line not before Console.log(name);

👀 Let, Const can also be hoisted but their hosted is specific to block as they are block scope variables. 


So now as of ES6 Hoisting has both functional and block scope. 

------------------------------------------------------------------------------------------------------------------------
Q24. What is event propagation in JavaScript?

 Answer:

When ever we have parent child dom elements structure and some event occurred at some level then both event bubbling or event capturing concept happen. Event bubbling is default behavior of modern browsers. 

Combination of both event bubbling  and event capturing is known as event propagation.
------------------------------------------------------------------------------------------------------------------------
Q25. Is JavaScript single threaded or multi threaded?

Answer: 

JavaScript is single threaded and synchronous by default

No comments:

Post a Comment