JavaScript Flashcards
What is JavaScript?
A scripting language for creating dynamic web page content.
ecmascript
A standard on how a language should work (ex. javascript) and what features it should have.
Destructuring
a faster way to access data from arrays, objects, and maps by setting each value as new variables.
map (array method)
map creates a new array by calling a function on every array element. It does not change the original array.
spread operator (ES6) (…)
allows you to quickly copy all or part of an existing array or object into another array or object using “…”
rest operator (…)
allows a function to take an indefinite amount of arguments and bundle them in an array. The rest parameter must be placed at the end of the arguments to gather all remaining arguments.
preventDefault (method)
method that tells the form we’ll handle what happens when a form is submitted. By default, when a form is submitted, the information is sent through a URL.
named export vs default export
named export must use the same name. For default, you can come up with any name. You can only have one default export per file. example for named export: “export const books …”. example for default export: “export default books”.
named import vs default import
named import must be the same name. for default export, we can come up with whatever we want. example- for named import: “import {books} from “./books.js””. example for default import “import banana from “./books.js””.
synchronous
Tasks are completed one after another. JavaScript is synchronous and single-threaded.
asynchronous
Tasks are completed parallel to one another. This is a lot better performance-wise than synchronous. In JS, async is provided by Events. Async patterns include - callbacks, promises, and async/await.
Blocking
Blocks the execution. This is synchronous.
Non-blocking
Does not block the execution of further-operations. This is mainly asynchronous. Non-blocking is more performant than blocking code.
event
An event is JS interaction with HTML elements. Such as when the page loads, or when a button is clicked.
event loop
A constantly running process that coordinates the tasks between the call stack and callback queue to achieve concurrency.
callback queue
A queue that keeps all callback functions ready for execution and executes after the call stack is empty. This operates on the FIFO principle. The callback functions are pushed to the call stack to execute when the event loop finds an empty call stack.
callback
a function passed as an argument to another function.
promise
an object that represents the eventual success or failure of an asynchronous task and its resulting value.
.then() promise method
When the promise is resolved/fulfilled, you can access the data and display the data within this method. It immediately returns an equivalent Promise object, allowing you to chain calls to other promise methods- creating a promise chain.
.catch() promise method
When a promise is rejected/fails, you can access the error information and display the error information in this method. It immediately returns an equivalent Promise object, allowing you to chain calls to other promise methods- creating a promise chain.
.finally() promise method
The last stage of the promise is where the promise has “settled”. This is useful when you want to do something when the promise has settled.
.all() promise method
This takes multiple promises as input and returns a single Promise. The returned promise is fulfilled if all promises are able to be resolved. It rejects when any of the input’s promises rejects, returning the first reject. This runs the inputs in parallel so it is performant.
fetch() method
Used to request data from a server. The request can be any type of API that returns the data in JSON or XML. It requires one parameter, the URL to request, and returns a promise.
fetch(URL) // api for the get request
.then(response => response.json())
.then(data => console.log(data));
async/await
async/await allows you to write asynchronous code using the async and await keywords. The async keyword is used to declare a function as asynchronous, while the await keyword is used to pause the execution of a function until a promise is resolved. Async returns a promise.
When setting up async functions, it is good practice to setup try…catch within the function to catch any errors.