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
    3Topics
    |
    3 Quizzes
Lesson 5, Topic 4
In Progress

JavaScript: Block Scope

Yasin Cakal 10 Nov 2021
Lesson Progress
0% Complete

Block scope is the next context we’ll look at. When a variable is defined inside a block, only the code within the curly braces{ } has access to it. Because it is only accessible to the lines of code within that block, we say that variable has block scope.

Variables declared with block scope are referred to as local variables because they are only accessible by code within the same block.

This is how block scope works:

const returnDay = () => {
  let day = 'Monday';
console.log(day); 
};
returnDay();        //Output: Monday
console.log(day);   //Output: ReferenceError

Notice the following:

  • We create a function called returnDay().
  • The day variable is only available within the function’s curly braces { }.
  • It throws a ReferenceError if we try to log the same variable outside of the function returnDay().

Responses

Leave a Reply

%d bloggers like this: