local and global Scope in JavaScript | JavaScript Tutorial in Hindi #88

CodeWithHarry
28 Oct 202204:27

Summary

TLDRIn this video, the presenter explains the concepts of local, global, and functional scope in JavaScript with practical examples. The discussion begins with the block-level scope using 'let' and 'var', highlighting the differences and the resulting behaviors in code. The presenter then demonstrates functional scope by showing how variables declared inside functions are inaccessible outside. Finally, the global scope is explained with examples of variables accessible throughout the entire script. The video clarifies the distinctions between block, functional, and global scopes, emphasizing their importance in JavaScript programming.

Takeaways

  • 😀 Scope in JavaScript refers to the visibility and accessibility of variables within different parts of a program.
  • 🔍 The script introduces three primary types of scope: local (block and functional), global, and functional scope.
  • 📌 Local scope is further divided into block-level scope and functional scope. Block-level scope is introduced with 'let' and 'const', which are not accessible outside the block they are declared in.
  • 👀 The 'var' keyword declares a variable with global scope, meaning it can be accessed and modified anywhere in the script.
  • 🔑 The script demonstrates block-level scope by showing an error when trying to access a 'let' declared variable outside its block.
  • 🛠️ Functional scope is illustrated by variables declared within a function, which are not accessible outside of that function.
  • 💡 The video uses practical examples in the console to explain the concepts, such as declaring variables with 'let', 'var', and 'const'.
  • 🔄 The script clarifies the difference between local and global scopes, emphasizing that local includes both block and function scopes.
  • 📚 It is important to understand scope to avoid errors and unintended variable access in JavaScript programs.
  • 🔄 The video also touches on the concept of 'time pass' coding, which is discouraged in favor of efficient and meaningful coding practices.
  • 👋 The presenter invites viewers to subscribe to the JavaScript playlist for more educational content.

Q & A

  • What are the three primary scopes discussed in the video?

    -The three primary scopes discussed in the video are local (block-level), functional, and global scope.

  • Why does using 'let' in a script result in an error when trying to access the variable outside its block?

    -Using 'let' results in an error when accessing the variable outside its block because 'let' provides block-level scope, meaning the variable is only accessible within the block it is declared in.

  • What is the difference between 'let' and 'var' in terms of scope?

    -The difference between 'let' and 'var' is that 'let' has block-level scope, limiting the variable's accessibility to the block it is declared in, whereas 'var' has a function-level or global scope, depending on where it is declared.

  • Why does the video suggest not to use 'let a = 8' outside of a function or block?

    -The video suggests not to use 'let a = 8' outside of a function or block because it would be declared in the global scope, which can lead to potential conflicts or unintended side effects in larger applications.

  • What does the video mean by 'block-level scope'?

    -Block-level scope, as explained in the video, refers to the scope of a variable declared with 'let' or 'const', which is limited to the block (enclosed by curly braces) in which it is declared.

  • How does functional scope differ from block-level scope?

    -Functional scope refers to the scope of variables declared within a function, making them accessible only within that function. Block-level scope is more specific, applying to variables declared with 'let' or 'const' within any block of code.

  • What is the purpose of creating a function to demonstrate functional scope in the video?

    -The purpose of creating a function in the video is to show that variables declared within the function have functional scope, meaning they are not accessible outside of the function.

  • Can you access a variable declared with 'let' inside a loop from outside the loop?

    -No, you cannot access a variable declared with 'let' inside a loop from outside the loop because 'let' provides block-level scope, and the loop is considered a block.

  • What is the global scope in JavaScript, and how does it differ from local scope?

    -The global scope in JavaScript refers to the scope of variables that are accessible throughout the entire script, regardless of where they are declared. It differs from local scope, which includes both block-level and functional scope, and is limited to specific blocks of code or functions.

  • How does the video clarify the confusion between functional scope and local scope?

    -The video clarifies the confusion by explaining that local scope consists of both block-level and functional scope, which are limited to specific blocks or functions, whereas global scope variables are accessible anywhere in the script.

Outlines

00:00

💻 Introduction to Scopes in JavaScript

The video starts by introducing the topic of local, global, and functional scopes in JavaScript, with the promise of explaining these concepts with examples. The host invites viewers to follow along on their computer screens for a hands-on coding demonstration.

🔍 Exploring Block-Level Scope with 'let'

The host begins coding in a script.js file, demonstrating the use of 'let' to declare a variable with block-level scope. An error is explained as a result of 'let' being restricted to block scope, meaning variables declared with 'let' within a block cannot be accessed outside of it.

🔄 Understanding 'var' and Global Scope

By changing 'let' to 'var', the host shows how 'var' variables are globally scoped and not restricted to block scope. This allows the variable to be accessible outside of the block, contrasting with 'let'.

🔧 Functional Scope in JavaScript

The host introduces functional scope by creating a function and declaring a variable within it. They demonstrate how variables within a function are local to that function and cannot be accessed outside of it.

🌐 Exploring Global Scope

Global scope is demonstrated with a variable declared outside of any function or block. This variable can be accessed from anywhere in the code, highlighting the difference between global, functional, and block scopes.

🧩 Clarifying Local Scope

The host clarifies the concepts of local scope, explaining that it includes both block and functional scopes. They emphasize that while block scope is within blocks like loops or conditionals, functional scope is within functions.

📚 Summary and Conclusion

The video concludes with a summary of the different types of scopes in JavaScript: block, functional, and global. The host encourages viewers to access the JavaScript playlist for more content and thanks them for watching.

Mindmap

Keywords

💡Local Scope

Local scope in JavaScript refers to variables that are accessible only within the block or function where they are declared. In the video, the speaker explains that variables declared with 'let' or 'const' inside a block (e.g., within curly braces) cannot be accessed outside that block, exemplifying block-level scope.

💡Global Scope

Global scope refers to variables that are accessible throughout the entire script, outside of any specific block or function. In the video, the speaker demonstrates this by declaring a variable with 'var' outside any function or block, making it accessible from any part of the script.

💡Functional Scope

Functional scope refers to variables that are accessible only within the function where they are declared. In the video, the speaker shows that variables declared inside a function cannot be accessed outside of that function, which is a key aspect of JavaScript's scope rules.

💡Block-level Scope

Block-level scope is a type of local scope specific to blocks of code defined by curly braces. The video highlights that 'let' and 'const' declarations create block-level scope, meaning variables declared this way are only accessible within the block they are defined in.

💡let

'let' is a JavaScript keyword used to declare variables that are block-scoped. The video explains how using 'let' inside a block means the variable will not be accessible outside that block, as opposed to 'var' which has a broader scope.

💡var

'var' is a JavaScript keyword used to declare variables that are function-scoped or globally-scoped if declared outside any function. The speaker illustrates that variables declared with 'var' inside a block can still be accessed outside of that block, highlighting the difference from 'let' and 'const'.

💡const

'const' is a JavaScript keyword used to declare variables that are block-scoped, similar to 'let', but their value cannot be reassigned. The video briefly touches on 'const' in the context of block-level scope, emphasizing its similarity to 'let' in terms of scoping rules.

💡console.log()

The 'console.log()' function is used in JavaScript to print output to the console. Throughout the video, the speaker uses 'console.log()' to demonstrate the visibility and scope of different variables, making it easier to understand their accessibility.

💡Error

In the context of the video, an error occurs when trying to access a variable outside its scope. The speaker demonstrates this by showing how accessing a 'let'-declared variable outside its block leads to an error, underscoring the importance of understanding scope.

💡Variable Declaration

Variable declaration refers to the process of defining a variable in JavaScript using keywords like 'let', 'var', or 'const'. The video provides various examples of variable declarations and explains how the choice of keyword affects the variable's scope and accessibility.

Highlights

Introduction to JavaScript scope concepts with examples.

Demonstration of variable declaration using 'let' and its block-level scope.

Error explanation when trying to access a block-scoped variable outside its block.

Comparison between 'let' and 'var' in terms of scope.

Explanation of block-level scope with 'let' and 'const'.

Illustration of local scope within a function using 'let'.

Definition and demonstration of functional scope.

Clarification on the difference between functional and global scope.

Example of accessing a global variable from different parts of the code.

Differentiation between local and global scopes, including block and function scopes.

Summary of the primary types of scopes in JavaScript.

Encouragement to subscribe to the JavaScript playlist for further learning.

Conclusion of the video with thanks to the viewers.

Outro music signaling the end of the video.

Transcripts

play00:00

In this video, we will see

play00:01

What is the local, global, and functional scope?

play00:03

in JavaScript.

play00:04

We will understand all of this with examples

play00:06

Let's hop onto our computer screen and let's get started.

play00:08

*Intro Music*

play00:14

So guys now let's talk about the scope

play00:16

I will open the script.js file

play00:18

Because here I will do the primary coding

play00:20

and I will open the console also

play00:22

I will make you do the coding mainly in the console

play00:25

And let a(variable) is equal to

play00:28

if I write 8 here suppose

play00:30

I will write console.log() and pass a(variable)

play00:32

Then you all will say "Harry please don't do timepass"

play00:35

Okay?

play00:35

8 will be printed

play00:36

We should not do this much time pass

play00:38

But if I show you all one thing.

play00:39

And if I say I am doing time pass

play00:42

I am wasting my time right

play00:43

If I am being accused of wasting my time

play00:46

So now you let me know

play00:47

if I shift a equals to 8 here

play00:50

And now if I do console.log(a).

play00:52

So here....

play00:54

Why is an error coming

play00:56

Let me know you all, okay?

play00:57

So this error occurred because

play00:59

whenever we use let to declare a variable

play01:00

then the variable has a block-level scope, okay?

play01:03

And if I change this to var.

play01:05

I will show you

play01:06

If I change this to var and save it

play01:08

And if I reload this

play01:10

so here 8 will be printed, okay?

play01:12

And if I change this to let

play01:14

So what will happen?

play01:15

Then 8 will not be printed and error will occur

play01:18

So what is the reason for that?

play01:19

Let has a block-level scope

play01:21

And var has global scope

play01:23

That means in a If statement.

play01:26

or in a while/for loop you have used let

play01:28

Then after coming out of that block

play01:31

it will be deleted, okay?

play01:33

It will not be in existence.

play01:36

That means you will get an error

play01:37

So here in javascript

play01:39

There are primarily 3 scopes

play01:41

one of them is block-level that I showed you

play01:44

and others are functional and global scope.

play01:46

Let and const provide block-level scope

play01:48

which means variables declared inside a

play01:51

inside a curly bracket that is what written here

play01:54

cannot be accessed outside the block, okay?

play01:56

Like I showed you a(variable) won't be available here

play01:59

variables declared within a JavaScript function

play02:01

becomes local to the function, okay?

play02:03

If you create a function

play02:05

so outside that function you

play02:07

won't find that variable.

play02:08

So this is functional scope.

play02:09

So I will show you here

play02:11

uhh...

play02:12

If I create a function here

play02:13

and if I...

play02:14

create a variable

play02:15

Suppose if I create this a function

play02:19

function a, let a is equal to

play02:22

Let it be ax()

play02:23

and if I do console.log(a)

play02:27

and if I run function ax() here

play02:29

I will comment this out

play02:32

If I run function ax() here

play02:34

So I can see 8 here

play02:35

but at the same time if I do console.log(a) here

play02:38

I will just shift this line

play02:40

So just see the output for ax() will come here

play02:42

that is 8

play02:43

But a won't be defined

play02:44

Because it has a functional scope

play02:46

So what is global scope

play02:47

Global scope means that

play02:48

if I create a variable here

play02:50

Suppose let p is equal to 9

play02:52

So I can use p by doing using console.log(p)

play02:56

Okay?

play02:58

I will show you all

play02:59

If I make this then the value of p will be printed here

play03:02

And this p variable

play03:05

I can print it here too, okay?

play03:07

Uhh...

play03:08

I will just reload it here

play03:10

So you just see I can print p here too

play03:12

Now you all don't be confused

play03:13

In functional scope and local scope, okay?

play03:17

Now you see when block scope

play03:18

is in the block scope

play03:19

functional and block both are local scopes, okay?

play03:22

Both are local variables.

play03:23

Block scope is used when

play03:25

when we use a block

play03:27

and we use a functional scope when

play03:29

when this thing is in a function, okay?

play03:31

So you all don't be confused

play03:33

Primarily there are two types of scope

play03:36

local and global

play03:37

local consists of block and function

play03:39

and you can see here

play03:41

How do they work

play03:42

Here I have given heading to local and global blocks

play03:44

But I have created here block, functional and global

play03:47

So basically what I mean is

play03:49

function, global and block scopes

play03:50

I am just mentioning this

play03:52

So that you won't be confused, okay?

play03:53

Local consists of block and function scope

play03:55

And rest is global

play03:56

I just wanted to make a quick video

play03:58

If you haven't subscribed to JavaScript playlist

play04:01

Sorry if you haven't accessed this

play04:02

So please access it

play04:03

This was all for this video

play04:05

Thankyou so much guys for watching this video

play04:07

And I will see you next time.

play04:08

*Outro Music*

Rate This

5.0 / 5 (0 votes)

Related Tags
JavaScriptScopeTutorialCodingProgrammingBeginnerExamplesFunctionsVariablesWeb Development