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

JavaScript: Blocks and Scope

Yasin Cakal 10 Nov 2021
Lesson Progress
0% Complete

We need to talk about blocks before we can talk about scope.

We’ve seen blocks in functions and if statements before. The code contained within a set of curly braces { } is referred to as a block. Blocks serve as a structural marker for our code, allowing us to group one or more statements together.

A function could be written as a block of code, as shown below:

const logSunColor = () => {
  let color = 'yellow'; 
  console.log(color);  
}

It’s important to note that the body of the function is actually a block of code.

Consider the following block in an if statement:

if (true) {
  let day= "Sunday"; 
   console.log(day);
}

//Output: Sunday

We’ll see how blocks define the scope of variables in the coming exercises.

Responses

Leave a Reply

%d bloggers like this: