What will be the Output?

Front end Interview

In many interviews you will be asked to assess the code snippet

Here I have the few samples which can prepare you for these kind of interview questions

1) Scope related question – If we execute this Javascript, what will the browser’s console show?

var text = 'outside';
 function logIt(){
     console.log(text);
     var text = 'inside';
 };
 logIt();

Answer: Output of this snippet will be undefined.

Reason: Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope before code execution. Inevitably, this means that no matter where functions and variables are declared, they are moved to the top of their scope regardless of whether their scope is global or local.