Scopes 1
Scopes 1
word
what do we mean
by
the
scopes?_?_
→ In
simple words , Slope is
simply where to look
① biloba
② function
③ block
Global
scoff
→
if a variable is
present in a
global slope ,
then it is accessible
everywhere
in the
JSfiᵈ
how
But do we
define a variable in
global
there the
Slope are
many ways
→ one
of way
is
to / define
declare variable outside
any functions
orabl_
functioned
In a
function ,
the
visibility of a variable / fumi
is
just inside the outer
functions
L
function funll
van a = to ; 3 here n is just accessible
inside can
fun or cue
say
3
it is local tuff
Peek Stolpe
In Is we can use a
pair of curly braces to
declare a block .
→ if else 61 .ee
&
: ::
→f1 while
for block
block
} saw
b1
Now
if variable/
fine is
only accessible / visible
"
a
block
inside a then it well he
hang block
Scoff
?⃝
Let / liar , const
if we use it outside a
function ,
matter
no
if
it is enclosed in a block or not
,
it will give the
variable
global Scoff
let → whenever we initiative a valuable with let ,
it
alienage gets the
scope of enclosing B_
if we declare ,
let outside
any
block , then
it does not
get complete global scope
let doesn't allow ieedeclaraticn ,
but var does
allow
let
if you
initiative a variable with outside
but
if you
do with var , it will
kg
let)
has block scope (same
const → it also as
,
allow
the
only difference is it doesn't
reassignment
.
const -10
g-
y
= 22 :
Xeric
How JS
, pauses the code_
So we know that JS is not
interpreted ,
it
So enlcute code
try
whenever we to a JI ,
JS
one
of the
following ways
-
1) either it will be
getting
a vanlue
a
assigned
used
i. e. it is as a
target 2=10
value
2) or it will be used to retrieve a i. e
↑ '
console In)
.ly ↑
will
what JS does is
,
it start the
pausing
global
Outside it
phase .
Sio 're
,
score 91cm : ←
→ will just
umʰ
we
do scope
absolution
¥ =
phase 2→executi_
declaration
=
also formal
In the
parsing phase JS looks
for formal
declaration
only
allocate
in Hus
parsing phone we
only
Stolpe values variable
of the
not .
,
=
the moment we
go
in a
fund ,
we maintain a
new
scope as well i e
- -
fund scoff
l¥m# global
phase -1 →
parsing
→ scope of fun
↑ phase -
2. →
eneutn__
→
p 1
teak
→
faint .
→
→ global
-☒-
- -
→ scope off] phase -
=
I -
pansy
-¥ʰ →phaI_
pu
enecutien
team
vi
Case I
"
this was ^
declared outs -
÷
.
a it was
vibhau Case 2 →
declared outside
once inside a
scope ,
we don't know about slope of
a variable we check the outer
sofas are
begat
Autogbobals → In JS
if we
keep an
seemly
variable in outer scopes &
scope of a no
where
fend it
,
we
automatically consider
this
happens deny
global staff
it in
enecuhar
pha#
→ auto
global only works week
target reference
& not
SOI
global
scope
→
of ten team
✗ ✗✗ erred
☒¥gⁿE
phas#
scope of fun part
→
Tu#-
sur m
defend
Klint
¥va
&# ʰdefud_