0% found this document useful (0 votes)
38 views

Codechum 20

The document discusses JavaScript variable scope through examples of global, local, and nested scopes. It shows how variables can be accessed, modified, reassigned, and shadowed within different scopes.

Uploaded by

arvsimisimi
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Codechum 20

The document discusses JavaScript variable scope through examples of global, local, and nested scopes. It shows how variables can be accessed, modified, reassigned, and shadowed within different scopes.

Uploaded by

arvsimisimi
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

//Global Scope Access

let message = "Hello";

function printMessage() {
console.log(message);
}

printMessage();

//Local Scope Access

function greet(name) {
let greeting = "Hello, ";
console.log(greeting + name);
}

greet("Alice");

//Global Variable Modification

let count = 5;

function increment() {
count += 1;
console.log(count);
}

increment();

//Variable Reassignment

function multiply(num1, num2) {


const result = num1 * num2;
console.log(result);
}

const inputNum1 = parseFloat(prompt("Enter the first number: "));


const inputNum2 = parseFloat(prompt("Enter the second number: "));

multiply(inputNum1, inputNum2);

//Nested Local Scopes

function outerFunction() {
let outerVariable = 5;

function innerFunction() {
let innerVariable = 10;
console.log("Inner Variable:", innerVariable);
}

console.log("Outer Variable:", outerVariable);


innerFunction();
}

outerFunction();

//Shadowing Parameter

function calculate(paramX) {
let localX = 5;
let result = paramX * localX;
console.log(result);
}

const inputParamX = parseFloat(prompt("Enter a number: "));


calculate(inputParamX);

You might also like