JavaScript Flashcards
What is a lexical environment.
A place where variables and functions live during program execution.
What is closure?
-Scopes can access values in their parents but not their children
function outside() { let x = 2 return function inside(y) { return x * y } }
What is an Immediately Involed Function Expression (IIFE)
-Enable to execute code once without cluttering global namespace and are not declared global .Good for a page with many event listeners (function () { // body of the function }());
What is a pure function?
- Relies only on input and no external variable or data
- Same input same deterministic output
- Doesn’t change the inputs
- No side effects, just wraps copyiable clean code
- Can’t reach outside world like databases or files
function addElementToArray(a, element) { retun [...a, element] }
Does the scope of let outside of a function cause sideeffects?
-Yes
What is hoisting?
-
What is the difference between var and let?
- Var was always part of JavaScript
- function block, dies at the end of function
- var gets hoisted so if called too early it is undefined but exist in lexical scope no, so no error
- let was introcuted in 2015 ES6 so old not upgraded browsers won’t work with it
- let has block scope, dies at the end of the defined block
- let does not get hoisted so if called to early it will create a referenceerror
What is the difference between == and ===?
-Both are comparism operators
== compares values only
‘1’ == 1 is true
converts both to the same type
=== compares value and type
‘1’ == 1 is false
What is the difference between let and const?
-Both are used to assign variables
-const is there to define a variable only once and not reassign it
const c; will defined it to undefined and you can’t change it back
-when const is an array you can modify the object array but you can’t reasign the whole variable
-let allows you to reassing a varable
What is the difference between null and undefined?
-Both represant empty value
-undefined is a placeholder automatically assigned to variables
-undefined should not be assigned manually
typeof(undefined) => undefined
-null is set on purpose to variables to empty them
typeof(null) => object
What is the use of arrow functions?
- A function will set its this to the outwards scope which can be window
- To avoid this an arrow function can be used because it doesn’t have its own this
What is prototypal inheritance?
- Every object has a property called prototype
- When new objects are created from these objects they will inherence their properties
- First is looks at its own props if it is not there it will inherance it
What is the difference between function declaration and function expression?
console. log(funDecl());
console. log(funExpressl());
function funDecl() { console.log('available before declared'); };
let function funExpressl() {
console.log(‘errors when called before’);
};
-has variable scope and behaves like variable
What is Promises and why do we use it?
-Whenever you make an async call you have to wait for an answer
-This answer can work or does not work so you can use callbacks
-But if you need multiple asyncs you don’t really want to stack callbacks as it gets ugly
-Promises are helpful here as they are clean and easy to work with
.then will execute only when the call was successful
.catch will executre when it runs into an error
What is the output order?
setTimeout(function() { console.log('a'); }, 0); console.log('b'); console.log('c');
b c a
Because setTimeout will make the function asynchronous so even waiting 0 will take longer than the next line as it waits for function stack