Javascript - filter, includes, forEach
1. filter - return Array
FYI. map - also return Array
const numbs = [10, 20, 30];
const callBack = (n) => n > 15;
const biggerThan15 = numbs.filter(callBack);
console.log(biggerThan15); //[ 20, 30 ]
2. forEach - doesn’t return array
The forEach() method executes a provided function once for each array element.
let maybeJs = "Javascript";
let js = ["js", "jscript"];
if (!js.includes(maybeJs.toLowerCase())) {
js.push(maybeJs.toLowerCase());
}
console.log(js); //[ 'js', 'jscript', 'javascript' ]
3. filter - return boolean
The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate.
let maybeJs = "Javascript";
let js = ["js", "jscript"];
if (!js.includes(maybeJs.toLowerCase())) {
js.push(maybeJs.toLowerCase());
}
console.log(js);