Execution Context

JavaScript Execution Context In JavaScript, everything happens within an execution context. JavaScript is a synchronous, single-threaded language, meaning it executes code one line at a time in a sequential manner. Components of the Execution Context JavaScript’s execution context consists of two main components: Memory Component Stores variables, objects, and functions as key-value pairs. Code Component Executes code line by line. What Happens When You Run a JavaScript Program? When you run a JavaScript program, an execution context is created....

July 30, 2024 · 2 min

Hoisting

Hoisting in JavaScript Hoisting allows you to use functions and variables in your code even before they’ve been declared. However, the behavior differs based on how the variable or function is declared. Function Hoisting Functions declared using function declarations are fully hoisted. This means you can invoke a function before it appears in your code. sayHello(); // Output: "Hello!" function sayHello() { console.log("Hello!"); } In this case, the function is fully hoisted, so it works even when called before its definition....

July 29, 2024 · 2 min

Functions in Execution Context

Execution Context and Flow of the Program Consider the following JavaScript code: var x = 1; a(); b(); console.log(x); function a() { var x = 10; console.log(x); } function b() { var x = 100; console.log(x); } When this program runs, the execution flow is managed by the Global Execution Context (GEC) and function-specific execution contexts. Let’s break it down: 1. Global Execution Context (GEC) When the program starts, the Global Execution Context (GEC) is created....

July 28, 2024 · 3 min

Scope & Scope Chain

what is Scope Scope is the code block where you can access the particular variable/function. Lexical environment Lexical environment of a execution context is it’s local memory + lexical environment of it’s parent. what is Scope chain ? when an execution context is created, a lexical environment is also created for this context. when an identifier is not found in lexical environment, it tries to find it in it’s parent’s lexical environment....

July 27, 2024 · 1 min

Scope of let and const

TL;DR console.log(b); // 100 let a = 10; var b = 100; console.log(a); // referenceError: can't access a before initialized. let a = 10; var b = 100; In case of var scope is global, but for let and const they have different memory block that we can’t access before we put some values in it. Time since when let variable is hoisted till its assigned is know as Temporal dead zone....

July 26, 2024 · 3 min

Closure

what is Closure? A closure is the combination of a function bundled together with references to its the lexical environment. function x(){ var a = 7; function y() { console.log(a); } y(); } x(); // 7 what about this code, function x() { var a = 7; return function y() { console.log(a); }; y(); } var z = x(); z(); // still 7 because, function x() returns closure (not only function) so it still remembers the reference to the variable a , hence logs “7”....

July 25, 2024 · 3 min

Types of Functions & First Class Functions

Types of Function Declarations // Function statement aka function declaration function a(){ console.log("a called"); } // Function Expression var b = function () { console.log("b called"); } // anonymous Functions function () { // this gives error } // Named function expression var c = function x(){ console.log("jsfs"); } c(); // valid x(); // ReferenceError: x not defined // Difference between parameter & arguments function f(param1, param2){ console.log("f called") } f(argument1, argument2); // First class functions const fcf(function () { clg("passed") }) { return function(){ clg("return") } } what is a Callback functions functions that are passed as argument in a function, is know as callback function....

July 24, 2024 · 1 min