Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Scopes | Mastering Functions
Introduction to JavaScript
course content

Kursinnhold

Introduction to JavaScript

Introduction to JavaScript

1. Getting Started
3. Conditional Statements
4. Mastering Functions
5. Exploring Arrays
6. Discovering Loops

book
Scopes

Note
Definition

A Scope is simply an area in the code where a variable can be accessed or used.

There are two types of scopes:

  1. Global Scope;

  2. Local Scope.

If a variable is defined inside a block of code (between curly brackets {}), it is said to have a local scope. This means that it can only be accessed from inside that function or code block, or any nested blocks:

123456789101112
function exampleFunc() { let exampleVariable = 10; console.log(exampleVariable); // Valid if(10 + 10 == 20) { console.log(exampleVariable); // Valid } } exampleFunc(); console.log(exampleVariable); // Shows error
copy

A variable that is defined outside of any code block is said to have a Global Scope, and it can be accessed from anywhere:

123456789101112
let exampleVariable = 10; function exampleFunc() { console.log(exampleVariable); // Valid if(10 + 10 == 20) { console.log(exampleVariable); // Valid } } exampleFunc(); console.log(exampleVariable); // Valid
copy

A variable defined in a lower (nested) scope cannot be accessed from a higher (parent) scope:

1234567891011
function exampleFunc() { if(10 + 10 == 20) { let exampleVariable = 10; console.log(exampleVariable); // Valid } console.log(exampleVariable); // Shows error } exampleFunc(); console.log(exampleVariable); // Shows error
copy
question mark

Which of the following statements about variable scope is true?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 4. Kapittel 4

Spør AI

expand
ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

course content

Kursinnhold

Introduction to JavaScript

Introduction to JavaScript

1. Getting Started
3. Conditional Statements
4. Mastering Functions
5. Exploring Arrays
6. Discovering Loops

book
Scopes

Note
Definition

A Scope is simply an area in the code where a variable can be accessed or used.

There are two types of scopes:

  1. Global Scope;

  2. Local Scope.

If a variable is defined inside a block of code (between curly brackets {}), it is said to have a local scope. This means that it can only be accessed from inside that function or code block, or any nested blocks:

123456789101112
function exampleFunc() { let exampleVariable = 10; console.log(exampleVariable); // Valid if(10 + 10 == 20) { console.log(exampleVariable); // Valid } } exampleFunc(); console.log(exampleVariable); // Shows error
copy

A variable that is defined outside of any code block is said to have a Global Scope, and it can be accessed from anywhere:

123456789101112
let exampleVariable = 10; function exampleFunc() { console.log(exampleVariable); // Valid if(10 + 10 == 20) { console.log(exampleVariable); // Valid } } exampleFunc(); console.log(exampleVariable); // Valid
copy

A variable defined in a lower (nested) scope cannot be accessed from a higher (parent) scope:

1234567891011
function exampleFunc() { if(10 + 10 == 20) { let exampleVariable = 10; console.log(exampleVariable); // Valid } console.log(exampleVariable); // Shows error } exampleFunc(); console.log(exampleVariable); // Shows error
copy
question mark

Which of the following statements about variable scope is true?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 4. Kapittel 4
Vi beklager at noe gikk galt. Hva skjedde?
some-alt