Introduction to Javascript 2
Understanding Scope
The scope of a variable controls where it can be accessed from in the program. It can be of
- Global Scope
- Function scope
- Block scope ( if / for etc )
Variable defination
Variables can be defined with var, const and let keywords.
varkeyword is function scopedletandconstkeyword are block scoped.
this keyword
- Different from how other languages use this.
- In JS land, value of this depends on how a function is called.
callandapplyhelps in setting correctthisvalue- the difference is in the way the arguments are provided
applytakes a array of arguments whereascalltakes in individual arguments.
Hoisting
- All variable declarations are hoisted on the top of their scope.
- Hence they are processed before the code gets executed.
varis set as undefined and can be accessed before declaration.letandconstcannot be accessed and throws a referenceError if tried.
Sources