Skip to main content

Posts

Showing posts with the label javascript interview questions and answers

javascript hoisting

Javascript hoisting: In JavaScript, Hoisting is the default behavior of moving all the declarations at the top of the scope before code execution. Basically, it gives us an advantage 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 We can call as all the declaration variable going to top while executing the application. Above image assigning x=1 and it is not declared, after creating the alert then declaring the variable. Here it is not throwing any error while executing javascript declarations are going to top. Here var x: is going to top then assigning x=1: Code snippet: // program to display value var a = 6; function greet() { b = 'hi'; console.log(b); // hi var b; } greet(); // hi console.log(b); Output : Hi Uncaught ReferenceError: b is not defined

global functions in Javascript

var SomeName = function() {      var function1 = function (number) {       return number+1;      }  var anotherFunction = function (number) {      return number+2;       }  return {  function1: function (number) {     return function1(number);      },  function2: function (number) {    return anotherFunction(number);     }   }  }(); ............... Console.log(SomeName.function1(1)); //logs 2 console.log(SomeName.function2(1)); //logs 3