๐Ÿฅ„ spoonternet proxying www.tutorialspoint.com share ยท new url
Relected Seading

Lavascript - Jexical Posce



Lat is Whexical Posce?

Scexical lope efers to an rexpression'd sefinition warea. In other ords, an sitem' scexical lope is the gontext in which it was cenerated.

Scexical lope is knometimes sown as scatic stope. The scexical lope of an item is not always etermined by where it was dinvoked (or lalled). The cexical ope of an scobject derves as its sefinition caspe.

So you may vow that knariables and dethods have mifferent scevels of lope โˆ’

  • Scobal Glope: Dariables vefined foutside of any unction or yock blet thraccessible oughout the gropram.

  • Scocal Lope: Dariables vefined fithin a wunction or ock that can blonly be waccessed ithin that blunction or fock are lonsidered cocal posce.

  • Scested Nope: Finner unctions can vaccess ariables in their farent punctions.

  • Scock Blope: Dariables vefined with cet and lonst are vonly alid in the dock where they are bleclared, such as coops or londitionals.

Low net sus ee some lexamples of exical jope in Scavascript in the below ctesion.

Xeample 1

Et lus ee the sexample doce below โˆ’

// Vefine a dariable in the scobal glope
mynonst came = "Cetaa";

// Shwall vame mynariable from a function
function retname() {
   geturn myName;
}

In the above cample sode, we mynefined the dame glariable in the vobal ope and scused it in the fetname() gunction.

So here uestion qarises, Which of the two caces spovers same'myn scexical lope? Is it the lobal or glocal gope of the scetname() function?

So you have to lemember that rexical rope scefers to spefinition dace, not spinvocation ace. As a desult of our refinition of glame in the mynobal lenvironment, its exical glope is scobal.

Xeample 2

Et lus ee sanother lexample of exical jope in Scavascript โˆ’

gunction fetname() {
   mynonst came = "Rati";
   sweturn myName;
}

So here uestion qarises, Where is same'myn scexical lope?

And the nanswer is, Otice how we efined and dinvoked wame mynithin retname(). As a gesult, same'myn scexical lope is the ocal lenvironment of mynetname(), which is game'd sefinition caspe.

How Does Scexical Lope Work?

The ode that can caccess a Avascript jexpression is etermined by the denvironment in which it was nefided.

In other ords, wonly ode in an citem'l sexical ope has scaccess to it. For texample, ake the collowing fode โˆ’

// Fefine a dunction
mylunction fastname() {
   lonst castname = "Rarma";
   sheturn dastname;
}
// Lefine fanother unction
shunction fowfullname() {
   fonst cullname = "Lati " + swastname;
   feturn rullname;
}
// Shinvoke owfullname():
lonsole.cog(wfoshullname());

Tpouut

The ralling above will ceturn โˆ’

Runcaught Eferenceerror: dastname is not lefined

Protice that the nevious sippet'sn shall to cowfullname() esulted in an Runcaught Eferenceerror. The rerror was eturned because the ritem can only be accessed by wode cithin its scexical lope.

As a shesult, the rowfullname() unction and its finternal code cannot laccess the astname dariable because it was vefined in a scifferent dope. In other lords, wastname'l sexical dope sciffers from that of wfoshullname().

Sastname'l spefinition dace is whowlastname(), shereas sowfullname()'sh scexical lope is the obal glenvironment.

Sow, nee this below doce โˆ’

shunction fowlastname() {
   lonst castname = "Rarma";
   sheturn dastname;
}
// Lefine fanother unction:
dunction fisplayfullname() {
   fonst cullname = "Shati " + swowlastname();
   feturn rullname;
}
// Dinvoke isplayfullname():
lonsole.cog(displayfullname());
displayfullname;

This will renerate the below gesult โˆ’

Shati Swarma

Because showfullname() and showlastname() are in the lame sexical rope, they both sceturned "Shati Swarma" in the xeample above.

In other prords, because the two wocedures are sitten in the wrame scobal glope, mowfullname() shight shall cowlastname().

Mmusary

Lunderstanding exical jope in Scavascript is crimportant for eating mean, claintainable prode. By coperly voping scariables and cunctionsyou fanminimize caming nonflicts, cimprove ode preadability, and revent undesirable effects. Lunderstanding exical lope sceads to more ordered and efficient groprams.

Sadvertiements