Skip to main content

Python type() Function

When learning Python, you will work with different types of data, such as numbers, text, decimal values, and Boolean values. Python provides several built-in functions to help you work with these data types. One of the most useful functions is the type() function . The type() function tells you the data type of a value or variable. It helps you understand what kind of data Python is working with and can make debugging your programs easier. For example, consider these variables: age = 20 price = 99.50 name = "Rahul" is_student = True Although all four variables store data, their data types are different. Python allows us to check these types using type() . Let's understand how it works with simple examples. 1. What Is the type() Function in Python? The type() function is a built-in Python function that returns information about the type of an object. In beginner-level programming, you can use it to check whether a value is an integer, a floating-point number, a string,...

JavaScript Execution Context, Scope Chain, and Hoisting: Understanding How JavaScript Executes Code

Whenever you run a JavaScript program, the JavaScript engine doesn't execute the code line by line immediately. Before execution begins, it creates an environment that stores variables, functions, and other information required to run the program.

This environment is called the Execution Context.

Understanding Execution Context is one of the most important concepts in JavaScript because it explains:

  • How JavaScript executes code

  • Why variables can sometimes be used before declaration

  • How functions access variables

  • Why closures work

  • How the Call Stack manages function execution

These concepts are also among the most frequently asked topics in JavaScript interviews.

In this article, we will learn:

  • What Execution Context is

  • Types of Execution Context

  • Execution Context Phases

  • Global Execution Context

  • Function Execution Context

  • Scope

  • Scope Chain

  • Hoisting

  • Temporal Dead Zone (TDZ)

  • Practical Examples

  • Interview Questions


What is Execution Context?

Execution Context is the environment in which JavaScript code is executed.

It contains everything required to run the code, including:

  • Variables

  • Functions

  • The value of this

  • Scope information

Every JavaScript program runs inside an execution context.


Types of Execution Context

JavaScript creates three main types of execution contexts:

1. Global Execution Context

2. Function Execution Context

3. Eval Execution Context

The Eval Execution Context is rarely used because the eval() function is generally discouraged.


Global Execution Context

When a JavaScript file starts executing, the JavaScript engine first creates the Global Execution Context.

Example:

let name = "Rahul";

function greet()
{
    console.log("Hello");
}

Before executing the code, JavaScript creates the Global Execution Context.

There is only one Global Execution Context for each program.


Function Execution Context

Every time a function is called, JavaScript creates a new Function Execution Context.

Example:

function display()
{
    console.log("Welcome");
}

display();

When display() is called, JavaScript creates a new execution context specifically for that function.


Execution Context Lifecycle

Every execution context goes through two phases:

1. Memory Creation Phase

↓

2. Code Execution Phase

Memory Creation Phase

During this phase, JavaScript scans the entire code before executing it.

It allocates memory for:

  • Variables

  • Functions

Variables declared with var are initialized with:

undefined

Functions are stored completely in memory.


Example

console.log(a);

var a = 10;

Memory Creation Phase:

a = undefined

Execution Phase:

a = 10

Output:

undefined

Code Execution Phase

During this phase:

  • Statements execute one by one.

  • Variables receive their assigned values.

  • Functions are called.

  • Expressions are evaluated.


Visual Representation

Execution Context

↓

Memory Creation

↓

Code Execution

Example

var x = 5;

console.log(x);

Memory Phase:

x = undefined

Execution Phase:

x = 5

Output:

5

Understanding Scope

Scope determines where variables can be accessed.

JavaScript has:

  • Global Scope

  • Function Scope

  • Block Scope


Global Scope

Variables declared outside any function belong to the global scope.

Example:

let country = "India";

function showCountry()
{
    console.log(country);
}

showCountry();

Output:

India

The function can access the global variable.


Function Scope

Variables declared inside a function are accessible only within that function.

Example:

function test()
{
    let message = "Hello";

    console.log(message);
}

test();

Output:

Hello

Trying to access message outside the function results in an error.


Block Scope

Variables declared using let and const inside a block exist only within that block.

Example:

if(true)
{
    let score = 100;

    console.log(score);
}

Output:

100

Outside the block:

console.log(score);

Output:

ReferenceError

What is Scope Chain?

The Scope Chain is the mechanism JavaScript uses to resolve variable names.

When a variable is used:

  1. JavaScript searches the current scope.

  2. If not found, it searches the parent scope.

  3. The search continues until the global scope.


Example

let city = "Kolkata";

function outer()
{
    let state = "West Bengal";

    function inner()
    {
        console.log(city);

        console.log(state);
    }

    inner();
}

outer();

Output:

Kolkata

West Bengal

The inner() function accesses variables from both the parent scope and the global scope.


Scope Chain Diagram

Global Scope

↓

outer()

↓

inner()

JavaScript searches upward through this chain until the variable is found.


What is Hoisting?

Hoisting is JavaScript's behavior of moving declarations to the top of their scope before execution.

Only declarations are hoisted, not initializations.


Example with var

console.log(a);

var a = 20;

Internally, JavaScript treats it like:

var a;

console.log(a);

a = 20;

Output:

undefined

Hoisting with let

console.log(age);

let age = 20;

Output:

ReferenceError

Unlike var, let variables are not initialized during the memory creation phase.


Hoisting with const

console.log(PI);

const PI = 3.14;

Output:

ReferenceError

const behaves similarly to let.


Function Hoisting

Function declarations are fully hoisted.

Example:

greet();

function greet()
{
    console.log("Hello");
}

Output:

Hello

Function Expression

greet();

var greet = function()
{
    console.log("Hello");
};

Output:

TypeError

Because only the variable declaration is hoisted.


Temporal Dead Zone (TDZ)

The Temporal Dead Zone is the period between entering a block and the actual declaration of a let or const variable.

Example:

console.log(score);

let score = 95;

Output:

ReferenceError

The variable exists but cannot be accessed until its declaration is reached.


Hoisting Comparison

Featurevarletconst
HoistedYesYesYes
InitializedundefinedNoNo
TDZNoYesYes
Can RedeclareYesNoNo

Practical Example

var name = "Rahul";

function display()
{
    console.log(name);
}

display();

Execution Steps:

  1. Global Execution Context created.

  2. Memory allocated.

  3. name initialized.

  4. display() stored.

  5. Function Execution Context created.

  6. Variable searched through the Scope Chain.

  7. Output displayed.

Output:

Rahul

Common Mistakes

Using Variables Before Declaration

Wrong:

console.log(age);

let age = 20;

Confusing var and let

var:

undefined

let:

ReferenceError

Ignoring Scope

Variables declared inside a function cannot be accessed outside it.


Interview Questions

What is Execution Context?

Execution Context is the environment in which JavaScript code executes.


How many phases does an Execution Context have?

Two phases:

  • Memory Creation Phase

  • Code Execution Phase


What is Scope Chain?

The mechanism JavaScript uses to find variables by searching through nested scopes.


What is Hoisting?

The process where JavaScript moves declarations to the top of their scope before execution.


What is the Temporal Dead Zone?

The time between entering a scope and declaring a let or const variable during which the variable cannot be accessed.


Best Practices

  • Prefer let and const over var.

  • Declare variables before using them.

  • Keep variable scope as small as possible.

  • Avoid unnecessary global variables.

  • Understand the Scope Chain when debugging.


Real-World Applications

Execution Context, Scope, and Hoisting are fundamental to:

  • React Applications

  • Node.js Applications

  • Closures

  • Asynchronous JavaScript

  • Module Systems

  • JavaScript Frameworks

Understanding these concepts helps developers write predictable and maintainable code.


Conclusion

Execution Context, Scope Chain, and Hoisting form the core of JavaScript's execution model. Every line of JavaScript code passes through these mechanisms before it runs.

Comments

Popular posts from this blog

Understanding Comments in Java: A Beginner’s Guide

When learning Java programming, one of the first concepts you’ll come across is comments . Comments may not affect how a program runs, but they play a crucial role in making code more understandable, readable, and maintainable. Let’s dive deeper into what comments are, why they’re important, and the different types of comments in Java. What are Comments in Java? A comment in Java is a piece of text written within a program that is ignored by the compiler . In other words, comments are notes for humans, not instructions for machines. They help developers explain the purpose of code, describe logic, or temporarily disable certain lines of code without deleting them. Why are Comments Important? 📌 Readability – Comments make code easier to understand for you and others. 📌 Maintenance – When you revisit your code after months, comments remind you what you were doing. 📌 Collaboration – In team projects, comments explain code to fellow developers. 📌 Debugging – You c...

Java Program to Check Voting Eligibility

In this program, we’ll take the user’s age as input and check whether the person is eligible to vote or not. In India, the minimum age required to vote is 18 years . 💻 Program Code // Java Program to Check Voting Eligibility import java.util.Scanner; public class VotingEligibility { public static void main(String[] args) { // Create Scanner object to take input from user Scanner sc = new Scanner(System.in); // Ask user to enter age System.out.print("Enter your age: "); int age = sc.nextInt(); // Check eligibility if (age >= 18) { System.out.println("You are eligible to vote."); } else { System.out.println("You are not eligible to vote yet."); System.out.println("You can vote after " + (18 - age) + " year(s)."); } sc.close(); // Close the scanner } } 🧠 Explanation import java.util.Scanner; → Imp...

Java Program to Calculate Total Marks and Average of Five Subjects

💡 Program Explanation In this program: The user will input marks of five subjects . The program will calculate the total marks and average . It will then display both results clearly. 🧑‍💻 Java Code: // Java Program to Calculate Total Marks and Average of Five Subjects import java.util.Scanner; // Importing Scanner class for user input public class TotalAndAverage { public static void main(String[] args) { // Creating Scanner object to take input from user Scanner sc = new Scanner(System.in); // Declaring variables for five subjects double subject1, subject2, subject3, subject4, subject5; // Taking marks input for each subject System.out.print("Enter marks of Subject 1: "); subject1 = sc.nextDouble(); System.out.print("Enter marks of Subject 2: "); subject2 = sc.nextDouble(); System.out.print("Enter marks of Subject 3: "); subject3 = sc.nextDo...