Table of Contents
The if statement executes a statement if a specified condition is truthy. If the condition is falsy, another statement can be executed.
if (condition) {
statement1
} else {
statement2
}The while statement creates a loop that executes a specified statement as long as the test condition evaluates to true. The condition is evaluated before executing the statement.
while (condition)
statementThe for statement creates a loop that consists of three optional expressions, enclosed in parentheses and separated by semicolons, followed by a statement to be executed in the loop.
for ([initialization]; [condition]; [final-expression])
statementcallback function- is a function we pass to another function as an argumentpredicate function- the function returns eithertrueorfalse(a boolean value)anonymous function- a function that does not have a name (that is not stored in a variable)named function- a function that has a name (that is stored in a variable)implicit return- return the only expression without explicitly writing the word "return" (An implied return)Globally scopedvariables aren't declared inside of any code block, and are available anywhere in the program.Locally scopedvariables are only available inside of the code block they're declared within. A let i = 0 declaration inside of a for loop is a classic example– you won't be able to access that particular i variable outside of the for loop.
- In a function invoked with new,
thiswill be the constructed object. - In a function invoked with
.callor.apply,thiswill be the first argument passed to call or apply. - In a function created with
.bind,thiswill be the first argument passed to.bind. - In a (non-fat arrow) function declared as a method on an object,
thiswill be the object on which the method is declared. - If none of the above apply,
thiswill be either the global object, or undefined depending on whether'use strict'is enabled.
For each element in our array, call the provided function on that element.
const array1 = ['a', 'b', 'c'];
array1.forEach(element => console.log(element));- callbackFn - a function to run on each element in the array
a. element - the current element being looped over
b. index - (optional) the index of the element (i in a normal for-loop)
c. array - (optional) the array
forEachwas called upon
undefined
Returns a new array where each element has been changed from the original array in some way.
Note: The map function does not modify the original array.
const developers = ['John', 'Nathan', 'Nick']To uppercase each element
developer.toUpperCase()
const uppercaseDevelopers = developers.map(dev => dev.toUpperCase())['JOHN', 'NATHAN', 'NICK']- callbackFn - This function is called on each element in the array. The value that is returned is added to the new array.
Returns a new array, where each element is what was returned from the callback function.
Returns a new array with the elements that return true, when passed to our callback function.
const developers = ['John', 'Nathan', 'Nick']Filter for names that start with 'N'
// check if the first letter is N
dev[0] === 'N'
const developersStartsN = developers.filter(dev => dev[0] === 'N')['Nathan', 'Nick']- callbackFn - This function is run on each element. Return a truthy value to keep the element in the newly filtered array. Otherwise return a falsy value.
a. The callback parameters are the same as
forEachandmap
A new array, with elements that returned true when passed to the function.
If no elements return true, it returns an empty array.
Returns the index of the first element, that returns true
when passed out callback function. Returns -1 if no element returns true.
const developers = ['John', 'Nathan', 'Nick']Find the index of the first name that starts with 'N'
// check if the first letter is N
dev[0] === 'N'
const index = developers.findIndex(dev => dev[0] === 'N')1 (because Nathan starts with N)- callbackFn - A function to run on each element. If the element returns true, then
findIndexwill return theindexof that element.
The index of the first element that returns true when passed to our callback function. Otherwise -1
Return the first element that returns true when passed to our callback function.
Otherwise, return undefined.
const developers = ['John', 'Nathan', 'Nick']Find the first name that starts with 'N'
// check if the first letter is N
dev[0] === 'N'
const developerStartsN = developers.find(dev => dev[0] === 'N')'Nathan' // Because Nathan is the first element that starts with N- callbackFn - A function to run on each element. If the element returns true, then
findwill return thatelement.
The value of the first element that returns true when passed to our callback.
Otherwise returns undefined.
The some method returns true if any element in our array, returns `true when passed to the callback function.
Otherwise, if every element returned false. Then some will return false.
const developers = ['John', 'Nathan', 'Nick']Find out if any (some) name starts with 'N'
// check if the first letter is N
dev[0] === 'N'
const hasNameStartsN = developers.some(dev => dev[0] === 'N')true // Because Nathan's name starts with an N- callbackFn - A function to run on each element. If any (some) element returns true, then
somewill returntrue.
true if any element returns a truthy value when passed to our callback function.
false if every element returned a falsy value
The every method returns true if every element in our array, returns true when passed to the callback function.
If any element returns false, every returns false.
const developers = ['John', 'Nathan', 'Nick']Find out if every name starts with 'N'
// check if the first letter is N
dev[0] === 'N'
const everyNameStartsN = developers.every(dev => dev[0] === 'N')false // Because not every name starts with N. John does not start with 'N'- callbackFn - A function to run on each element. If
everyelement returnstrue, theneverywill returntrue.
true if the callbuck function, returns true for every element.
Otherwise, returns false