Back to Course

Introduction to JavaScript

0% Complete
0/0 Steps
  1. JavaScript: Basics
    9Topics
    |
    10 Quizzes
  2. JavaScript: Variables
    9Topics
    |
    10 Quizzes
  3. JavaScript: Conditional Statements
    10Topics
    |
    11 Quizzes
  4. JavaScript: Functions
    10Topics
    |
    11 Quizzes
  5. JavaScript: Scope
    6Topics
    |
    7 Quizzes
  6. JavaScript: Arrays
    11Topics
    |
    12 Quizzes
  7. JavaScript: Loops
    8Topics
    |
    9 Quizzes
  8. JavaScript: High Order Functions
    3Topics
    |
    4 Quizzes
  9. JavaScript: Iterators
    6Topics
    |
    6 Quizzes
Lesson 5, Topic 3
In Progress

JavaScript: Global Scope

Yasin Cakal 10 Nov 2021
Lesson Progress
0% Complete

The scope of our variables is the context in which they are declared. We think about scope in terms of blocks because variables can exist both outside and inside of these blocks.

Variables are declared outside of blocks in global scope. These variables are referred to as global variables. Because global variables are not bound within a block, any code in the program, including code in blocks, can access them.

Consider the following example of global scope:

const day = 'Monday';
const returnDay = () => {
  return day;  
};
console.log(returnDay());

//Output: Monday
  1. Despite the fact that the day variable is defined outside of the block, it can be accessed within it, giving it global scope.
  2. day can then be accessed through the returnDay function block.

Let’s look at how data can be accessed from anywhere in a program using global variables.

Responses

Leave a Reply

%d bloggers like this: