JavaScript Interview Flashcards
What is Scope?
Refers to the availability of variables in our code
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.
Can you redeclare variables with the let keyword
No, this is not possible.
Error: Syntax Error: Identifier ‘name’ has already been declared
What is Lexical Scoping?
means that a variable defined outside a function can be accessible inside another function defined after the variable declaration.
‘this’ refers to it’s current surrounding scope and no further.
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.
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.
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 difference between call and bind methods
instead of invoking the function, Bind returns a new function that you can invoke whenever you wish
What do you know about arrow functions?
Introduced in ES6
Concise Function syntax
Provides Lexical binding
Can’t be called with new keyword
Arrow functions do not bind their own “this” , instead, they inherit the one from the parent scope, which is called “lexical scoping”
What are Higher Order Functions?
This is a function that can take in another function
Ex. The .map() function is a higher order function that will run a callback on every item in the array and will push each result into a new array.
What are first class functions?
function that was built with the intention of being passed around to other functions. It does one specific thing, does not have side effects, and is not intended to be called directly, but rather, to be used by ‘other functions.’
What are Callbacks?
is a function passed into another function as an argument to be executed later.
What does the new keyword do?
- creates a new empty object { }
- sets the value of ‘this’ to be the new empty object
- calls the constructor method
What is a promise
A promise is an object that may produce a single value some time in the future : either a resolved value, or a reason that it’s not resolved
Pending, initial state, fulfilled, rejected
What are the differences between == and === ?
a===b means that a and b are equal in both value and type.
a==b means that they are equal in value only.
What is the difference between null and undefined.
Undefined means a variable has been initialized but not been assigned a value; //var initialized let a; //this log will give undefined console.log(a)
null is an assignment
//var initialized and assigned to null let a = null; //log will give null console.log(a)
Why use Promises?
Promises help us deal with asynch code in a far more simpler way compared to callbacks
What two methods/functions does the Promise Object provide access to?
.then() - called with promise.then()
.catch() - called with promise.catch()
And you would chain these together
What is Promise.all()
Allows us to query multiple APIs and preform some actions but only after all the APIS have finished loading
If one promise rejects, promise.all() will reject with an error message.
What are Classes?
Classes are like a blue print. Ex. Class could be an email for several users.
Every user has a different value for an email and email is the class.
What is a constructor function?
Creates new objects based upon the Class