JS Global Scope

HTML
CSS
C#
SQL

Global Scope Variable (var, let, const):

Declaration of Variables:

– Variables in JavaScript can be declared using var, let, and const keywords.
– var was traditionally used for variable declaration before the introduction of let and const in ES6.

Global Variables:

– Global variables are declared outside of any function or block scope.
– They are accessible from anywhere in the program, making them available to every function.

Usage of var for Global Variables:

– Before ES6, var was commonly used for declaring global variables.
– Variables declared with var are hoisted to the top of their scope.

Using let and const for Global Variables:

– With the introduction of ES6, let and const offer block-scoping, providing a better alternative to var.
– let allows reassignment, while const declares constants that cannot be reassigned.

Example:

Points to Remember:

– Using var for global variables is considered less preferable due to hoisting and the lack of block-scoping.
– let and const provide better scoping and should be preferred over var.
– Global variables can be accessed from any part of the program, but they should be used judiciously to avoid potential issues related to scope and naming conflicts.
– If you assign a value to a variable and forget to declare that variable, it will automatically be considered a global variable.

Course Video

Examples for Practice

You have to solve all the questions given below in the editor without copy-pasting.

1. Create a global variable username and write a function greetUser that logs a greeting message using the username.

– Create a Global Variable: Declare a variable called username in the global scope.
– Write a Function: Write a function named greetUser that logs a greeting message using the global variable username.

Here’s an example program:

// Step 1: Create a global variable username
let username = “John”;

// Step 2: Write a function greetUser
function greetUser() {
// Inside the function, use the global variable username to log a greeting message
console.log(“Hello, ” + username + “!”);
}

// Example usage
greetUser(); // Output: Hello, John!

Output

2. Create a global variable userDetails as an object with name and age properties. Write a function displayUserDetails that logs the details using the global variable.

– Create a Global Variable as an Object: Declare a variable called `userDetails` in the global scope, and set it as an object with properties name and age.
– Write a Function: Write a function named displayUserDetails that logs the details using the global variable userDetails.

Here’s an example program:

// Step 1: Create a global variable userDetails as an object
let userDetails = {
name: “Alice”,
age: 25
};

// Step 2: Write a function displayUserDetails
function displayUserDetails() {
// Inside the function, use the global variable userDetails to log the details
console.log(“Name: ” + userDetails.name + “, Age: ” + userDetails.age);
}

// Example usage
displayUserDetails(); // Output: Name: Alice, Age: 25

Output

3. Write a program that uses all three types of variable declarations (let, const, var) in different scopes – global scope, function scope, and block scope. Create a set of variables with the same name in each scope and try to log or modify them from different parts of the program. Observe and explain the behavior of each variable type in different scopes.

The task is to understand and demonstrate the behavior of different variable declarations (let, const, and var) in various scopes (global, function, and block).
1. Global Scope:
– Declare a variable with the same name using each type (let, const, and var) in the global scope.
– Try to log or modify these variables from different parts of the program.
2. Function Scope:
– Declare a function and inside that function, declare a variable with the same name using each type (let, const, and var).
– Try to log or modify these variables within and outside the function.
3. Block Scope:
– Declare a block of code (e.g., inside an `if` statement or `for` loop) and inside that block, declare a variable with the same name using each type (let, const, and var).
– Try to log or modify these variables within and outside the block.
4. Observe and Explain:
Observe and explain the behavior of each variable type in different scopes. Understand how they behave when trying to log or modify them from various parts of the program.

Here’s an example program:

// Global scope
let globalLetVar = “Global let”;
const globalConstVar = “Global const”;
var globalVarVar = “Global var”;

function exampleFunction() {
// Function scope
let functionLetVar = “Function let”;
const functionConstVar = “Function const”;
var functionVarVar = “Function var”;

if (true) {
// Block scope
let blockLetVar = “Block let”;
const blockConstVar = “Block const”;
var blockVarVar = “Block var”;

console.log(globalLetVar); // Access globalLetVar from block
console.log(functionLetVar); // Access functionLetVar from block
console.log(blockLetVar); // Log blockLetVar
}

console.log(globalConstVar); // Access globalConstVar from function
console.log(functionConstVar); // Log functionConstVar
// console.log(blockConstVar); // Error: blockConstVar is not defined outside the block

console.log(globalVarVar); // Access globalVarVar from function
console.log(functionVarVar); // Access functionVarVar from function
console.log(blockVarVar); // Access blockVarVar from function
}

exampleFunction();

Output