JavaScript Flashcards
What is Scope?
Refers to the availability of variables in our code
or
Scope dictates where JS engine should look for things
What is the scope of Var?
function scoped
What is the scope of let and const ?
block scoped
How are var declarations hoisted?
var declarations when hoisted they are initialized with undefined.
How are let and cont declarations hoisted?
when hoisted they are not initialized with undefined.
So, this would trigger a reference error if accessing before declaration.
What is Hoisting?
Hoisting means moving variable declarations to the top of their scope.
What are your use cases for variable declarations?
Try to use const declarations
If the value is expected to change, use let declaration
var declarations can be avoided as they introduce unnecessary confusion
Can you redeclare variables with the let keyword
No, this is not possible.
Error: Syntax Error: Identifier ‘name’ has already been declared
Does a function have access to global scope?
Yes, a function will always have access to its own function scope as well as the global scope.
A function can access variables defined with in the function as well as outside the function
What is Lexical Scoping?
means that a variable defined outside a function can be accessible inside another function defined after the variable declaration. But the opposite is not true; the variables defined inside a function will not be accessible outside that function.
‘this’ refers to it’s current surrounding scope and no further.
What is Scope Chain?
When we have nested functions, the JS engine for variable lookup starts with the inner most function where we are trying to access the variable and moves outward until it reaches the global scope
What is important to remember about Scope and nested functions?
Nested function have access to variables declared in their own scope as well as variables declared in the outer scope
What are Closures?
A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment).
In JavaScript, closures are created every time a function is created, at function creation time.
Further info:
This would let the function definition have an associated persistent memory which could hold on to live data between executions
What is Memoization
An optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached results when the same inputs occur again
What is Function Currying?
Currying is a process in functional programming in which we transform a function with multiple arguments into a sequence of nesting functions that take one argument at a time
function fn(a,b,c) transformed into fn(a)(b)(c)
What is ‘this’?
The JavaScript this keyword refers to the object it belongs to. You can set it with bind() among others.
‘this’ keyword is the execution context for a function call
It allows you to introduce reusability by allowing this value to be dynamic based on how a function is invoked
What is default binding
If call, apply, bind are not set, JS will default to the global scope and set ‘this’ keyword to the window object.
What are the five ways to invoke a function?
Implicit binding, Explicit binding, ‘new’ binding, default binding and Lexical Binding
Using implicit Binding, how do we know what ‘this’ is referencing?
When the function in invoked with the dot notation, the object to the left if that dot is what the this keyword is referencing.
Ex. person.sayMyName( )
‘this’ keyword is referencing the ‘person’ object
What is the first num called?
function myFunc(num){ return num } myFunc(2)
Parameter
What is 2 called?
function myFunc(num){ return num } myFunc(2)
Argument
What is the difference between call and bind methods
instead of invoking the function, Bind returns a new function that you can invoke whenever you wish