23 June, 2020
const strs = ["I", " ", "am", " ", "Iron", " ", "Man"];
const result = strs.reduce((acc, currentStr) => acc + str, "");
function map(arr, mapCallback) {
// First, we check if the parameters passed are right.
if (!Array.isArray(arr) || !arr.length || typeof mapCallback !== 'function') {
return [];
} else {
let result = [];
// We're making a results array every time we call this function
// because we don't want to mutate the original array.
for (let i = 0, len = arr.length; i < len; i++) {
result.push(mapCallback(arr[i], i, arr));
// push the result of the mapCallback in the 'result' array
}
return result; // return the result array
}
}
Higher-Order Function are functions that can return a function or receive argument or arguments which has a value of a function.
Class Inheritance: instances inherit from classes (like a blueprint — a description of the class), and create sub-class relationships: hierarchical class taxonomies. Instances are typically instantiated via constructor functions with the new
keyword.
Class inheritance may or may not use the class
keyword from ES6.
Prototypal Inheritance: instances inherit directly from other objects. Instances are typically instantiated via factory functions or Object.create()
. Instances may be composed from many different objects, allowing for easy selective inheritance.
&& is known as the guard operator ||: 1st expression is always outputted. The 2nd expression only gets outputted if the 1st expression is falsy.
&&: 1st expression is outputted if it's FALSY. The 2nd expression only get outputted if the 1st expression is truthy.
example
const truthy = true;
const falsy = false;
const beer = '🍺';
truthy || beer; // true
falsy || beer; // '🍺';
First whats a closure? A closure is an inner function that has access to the outer function’s variables.
In JavaScript, closures are created every time a function is created, at function creation time. To use a closure, define a function inside another function and expose it. To expose a function, return it or pass it to another function.
Webpack is a tool that will bundle your code and optionally all of its dependencies into a single .js file.
You control how assets are processed. If an image is below a certain size, you could base64 encode it directly into your Javascript for fewer HTTP requests. If a JSON file is too big, you can load it from a URL. You can require('./style.less') and it's automaticaly parsed by Less into vanilla CSS.
Webpack will slow you down at the start, but give you great speed benefits when used correctly.
Appending once means only one render so performance doesn't suffer.
note: Strings are immutable.
.split() will turn a string into an array
.toLowerCase()
.substring()
.startswith()
Object.entries()
note: Array useful methods .set() is a data structure that cant have duplicate items. const set = new `arr);
// flatten an array [1, [2], 3] use .flat()
forEach is an array method that we can use to execute a function on each element in an array. It can only be used on Arrays, Maps, and Sets.
arr.forEach(element => {
console.log(element);
});
When using forEach, we simply have to specify a callback function. This callback will be executed on each element in the array.
forEach() executes the callback function once for each array element; unlike map() or reduce() it always returns the value undefined and is not chainable. The typical use case is to execute side effects at the end of a chain.
map utilizes return values and actually returns a new Array of the same size.
The JavaScript for/in statement loops through the properties of an object:
for (variable in object) {
// do something
}
for...in is used to iterate over the enumerable properties of objects. Every property in an object will have an Enumerable value — if that value is set to true, then the property is Enumerable.
The JavaScript for/of statement loops through the values of an iterable objects such as Arrays, Strings, Maps, NodeLists, and more.
const products = ['oranges', 'apples'];
for (const [index, product] of products.entries()) {
console.log(index, product);
}
// 0, 'oranges'
// 1, 'apples'
//a function expression and so only defined when that line is reached
var functionOne = function() {
// Some code
};
//function declaration and is defined as soon as its surrounding function or script is executed (due to hoisting).
function functionTwo() {
// Some code
}
source: https://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname?rq=1
The purpose of "use strict" is to indicate that the code should be executed in "strict mode". With strict mode, you can not, for example, use undeclared variables.
A Document object represents the HTML document that is displayed in the window. The way a document content is accessed and modified is called the Document Object Model. see more here: https://javascript.info/modifying-document