local and global Scope in JavaScript | JavaScript Tutorial in Hindi #88
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
💻 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
💡Global Scope
💡Functional Scope
💡Block-level Scope
💡let
💡var
💡const
💡console.log()
💡Error
💡Variable Declaration
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
In this video, we will see
What is the local, global, and functional scope?
in JavaScript.
We will understand all of this with examples
Let's hop onto our computer screen and let's get started.
*Intro Music*
So guys now let's talk about the scope
I will open the script.js file
Because here I will do the primary coding
and I will open the console also
I will make you do the coding mainly in the console
And let a(variable) is equal to
if I write 8 here suppose
I will write console.log() and pass a(variable)
Then you all will say "Harry please don't do timepass"
Okay?
8 will be printed
We should not do this much time pass
But if I show you all one thing.
And if I say I am doing time pass
I am wasting my time right
If I am being accused of wasting my time
So now you let me know
if I shift a equals to 8 here
And now if I do console.log(a).
So here....
Why is an error coming
Let me know you all, okay?
So this error occurred because
whenever we use let to declare a variable
then the variable has a block-level scope, okay?
And if I change this to var.
I will show you
If I change this to var and save it
And if I reload this
so here 8 will be printed, okay?
And if I change this to let
So what will happen?
Then 8 will not be printed and error will occur
So what is the reason for that?
Let has a block-level scope
And var has global scope
That means in a If statement.
or in a while/for loop you have used let
Then after coming out of that block
it will be deleted, okay?
It will not be in existence.
That means you will get an error
So here in javascript
There are primarily 3 scopes
one of them is block-level that I showed you
and others are functional and global scope.
Let and const provide block-level scope
which means variables declared inside a
inside a curly bracket that is what written here
cannot be accessed outside the block, okay?
Like I showed you a(variable) won't be available here
variables declared within a JavaScript function
becomes local to the function, okay?
If you create a function
so outside that function you
won't find that variable.
So this is functional scope.
So I will show you here
uhh...
If I create a function here
and if I...
create a variable
Suppose if I create this a function
function a, let a is equal to
Let it be ax()
and if I do console.log(a)
and if I run function ax() here
I will comment this out
If I run function ax() here
So I can see 8 here
but at the same time if I do console.log(a) here
I will just shift this line
So just see the output for ax() will come here
that is 8
But a won't be defined
Because it has a functional scope
So what is global scope
Global scope means that
if I create a variable here
Suppose let p is equal to 9
So I can use p by doing using console.log(p)
Okay?
I will show you all
If I make this then the value of p will be printed here
And this p variable
I can print it here too, okay?
Uhh...
I will just reload it here
So you just see I can print p here too
Now you all don't be confused
In functional scope and local scope, okay?
Now you see when block scope
is in the block scope
functional and block both are local scopes, okay?
Both are local variables.
Block scope is used when
when we use a block
and we use a functional scope when
when this thing is in a function, okay?
So you all don't be confused
Primarily there are two types of scope
local and global
local consists of block and function
and you can see here
How do they work
Here I have given heading to local and global blocks
But I have created here block, functional and global
So basically what I mean is
function, global and block scopes
I am just mentioning this
So that you won't be confused, okay?
Local consists of block and function scope
And rest is global
I just wanted to make a quick video
If you haven't subscribed to JavaScript playlist
Sorry if you haven't accessed this
So please access it
This was all for this video
Thankyou so much guys for watching this video
And I will see you next time.
*Outro Music*
Посмотреть больше похожих видео
Scope Levels | Godot GDScript Tutorial | Ep 14
COS 333: Chapter 5, Part 2
Declaring Variables without Var, Let, Const - What Would Happen?
More Closure Examples | JavaScript 🔥 | Lecture 129
Javascript Interview Questions ( Var, Let and Const ) - Hoisting, Scoping, Shadowing and more
Learn Closures In 7 Minutes
5.0 / 5 (0 votes)