Javascript Flashcards
difference between let, var, const
var is global and function scoped, it can be re-declared and re-assigned
let is block-scoped, it will only be accessible within the block it was declared in, we cannot re-declare it but we can re-assign it
const must be declared with value as it cannot be re-assigned, const allows you to change properties of its value
What is responsive design
Responsive design is an approach to web page creation that makes use of flexible layouts, flexible images and cascading style sheet media queries.
The goal of this is to have web pages that detect users devices and screen sizes and adapt the layout accordingly.
What is restful API
REST stands for REpresentational State Transfer
REST is just a pattern for making API’s
Any API that is created in a REST pattern is a RESTful API
Every restful API should be able to use CRUD operations, create read update and delete with HTTP requests POST, GET, PUT, DELETE
EXTRA :
Architectural Constraints of RESTful API: There are six architectural constraints which makes any web service are listed below:
Uniform Interface Stateless Cacheable Client-Server Layered System Code on Demand
What is the difference between REST API and RESTful API?
REST is just an architectural pattern for designing restful API’s.
RESTful API is an API that follows this said pattern.
What is the point of RESTful API?
With API’s following one set architectural pattern, we can allow API’s to communicate with different programming languages and help web applications which rely on restful API’s to reside in different environments
What is closure?
Closures in JS allow us to have functions that hold private variables
Closure means, that when JavaScript runs your code, it looks for all variables in your functions, and if it sees a variable in a function that has no declaration (- let, const) inside of it, but in an outer scope (where the function is nested in) it “locks” the value for that variable inside that given
Famous closure example:
for( var i = 0; i < 3; i++) { const log = () => { console.log(i) } setTimeout(log,100) }
we would usually expect this function to print a console log of I every 100miliseconds
the output should be 0,1,2
But because the variable i is called with var, it’s hoisted to global scope and the for loop runs fully first before the log function is called, so it prints 3,3,3
With the variable called with let, we block scope it and it works properly
What is a promise?
A promise is an eventual completion of an asynchronous operation and its resulting value
A promise acts as a proxy for a value that isnt known when the promise is created
Promises allow us to work with async operations as though they are synchronous
Promises are either pending, fulfilled or rejected
What is a callback?
A callback is simply a function passed as an argument to another function.
What is asynchronous programming and why is it important in JavaScript?
Asynchronous programming makes it possible to express waiting for long-running actions without freezing the program during these actions. JavaScript environments typically implement this style of programming using callbacks, functions that are called when the actions complete.
How to compare objects?
if we know both objects are in the same order of keys but we want to check if the values are the same we can stringify both and compare with ===
we can check shallow equality by getting list of properties using Object.keys() then comparing each values, we can return true if they are all the same or false if they arent or are not of same length
if compared objects have nested objects then we have to do deep equality check by triggering a recursive call to check when property is a nested object
What is hoisting?
JavaScript Hoisting refers to the process whereby the interpreter appears to move the declaration of functions, variables or classes to the top of their scope, prior to execution of the code.
Hoisting allows functions to be safely used in code before they are declared.
Variable and class declarations are also hoisted, so they too can be referenced before they are declared. Note that doing so can lead to unexpected errors, and is not generally recommended.
What is apply, call and bind?
bind() - allows us to call a function but binding its ‘this’ value to another objects ‘this’
call(), apply() - allows us to call a function and explicitly state which ‘this’ to reference to while also allowing other arguments to be passed
the call method does not make a copy of the function it is being called on
call and apply are the same its just that apply asks for an array of arguments after ‘this’
var logPokemon = pokemonName.bind(pokemon); // creates new object and binds pokemon. ‘this’ of pokemon === pokemon now
pokemonName.call(pokemon,’sushi’, ‘algorithms’); // Pika Chu loves sushi and algorithms
pokemonName.apply(pokemon,[‘sushi’, ‘algorithms’]); // Pika Chu loves sushi and algorithms
What’s the difference between a variable that is: null, undefined or undeclared? How would you go about checking for any of these states?
null means the value points to no object, it is treated as a falsy for boolean operations, it is a js primitive value, null represents the intentional absence of any object value
undefined property indicates that a variable has not been assigned a value
undeclared is any variable that hasnt been assigned with let var or const - will throw a reference error
what is higher order function?
a higher order function is a function that receives functions as arguments and/or returns functions as output
For example, Array.prototype.map, Array.prototype.filter and Array.prototype.reduce are some of the Higher-Order functions built into the language.
what different between == and ===?
In summary; “==” abstract operator compares the values and then returns true or false. “===” strict operator not only checks the values but also the types of variables. If the values are not of the same type, “===” operator returns false because it does not do the necessary type conversions like the “==” operator does before it compares the equality.