βΉοΈ Use in order to make a string with variables (like Madlibs)
βΉοΈ Template strings use backticks, or "bent apostrophes": `Welcome back...`
alert( `Welcome back, ${firstName}, we missed you!` )
βΉοΈ Backticks can make combinations of strings and variables more readable.
// Write something like "Welcome Sue, your color for Tuesday is hsla(180 60% 30% / .5)":
// Normal quotes.
alert("Welcome " + name + ", your color for " + weekday + " is hsla(" + hue + " " + saturation + "% " + lightness + "% / " + alpha + ")");
// Backticks.
alert(`Welcome ${name}, your color for ${weekday} is hsla(${hue} ${saturation}% ${lightness}% / ${alpha})`);
// Old-school style function definition.
function showUserOnMap( latitude, longitude ) {
...
} ;
// "Variable" style function definition.
const user = "Fred" ;
const showUserOnMap = function( latitude, longitude ) {
...
} ;
// Cool-looking arrow style function definitions.
// You can write a single command on one line.
const showUserOnMap = ( latitude, longitude ) => ... ;
// You can enclose multiple lines of commands with braces {}.
const showUserOnMap = ( latitude, longitude ) => {
...
...
} ;
// You can omit the parentheses for a single parameter.
const showTimeZone = longitude => {
...
} ;
// Write empty parentheses if there's no parameter.
const getUserLocation = () => {
...
} ;
βΉοΈ Use when you're only calling a function in a single context (eg, inside an iterator like forEach).
βΉοΈ Basically a single-use, throwaway function π§».
// Old-school anonymous function definition, with parameter.
function( parameter ) {
...
} ;
// Old-school anonymous function definition, without parameter.
function() {
...
} ;
// Arrow style anonymous function definition, with parameter.
parameter => {
...
} ;
// Arrow style anonymous function definition, without parameter.
() => {
...
} ;
β Yay for simple syntax!
// Example of an anonymous arrow function in an array method.
myDivs.forEach(
div => alert( div.id )
) ;
β οΈ Can't use semicolons directly inside ()
// A semicolon inside array method parentheses () will break your code.
myDivs.forEach(
div => alert( div.id ) ; β
) ;
// Semicolons inside braces {} are fine though!
myDivs.forEach(
div => {
alert( div.id ) ; β
div.id.style.display = "block" ; β
}
) ;
The most general iterator, forEach executes any commands you want as it loops through your array.
// Say you want to alert each of your pets so you remember to feed them.
let myPets = [ "πΆ", "π±", "π°" ]
// π΄ The old school "for" loop is klunky.
for ( let counter=0; counter < myPets.length; counter++ ) {
alert( myPets[counter] ) ;
}
// π§βπ¨ The chic "forEach" iterator is simpler.
myPets.forEach(
pet => alert( pet )
) ;
β forEach syntax is way shorter!
βΉοΈ counter and pet are variables invented just for the loops and have no meaning outside.
β οΈ forEach doesn't work with collections of HTML tags (eg, getElementsByClassName) in older browsers.
β οΈ Can't break out of a forEach loop as you can a for loop.
β οΈ Hard to keep track of where you are in a forEach loop (no counter).
Returns a new, smaller array that matches the criteria returned in your embedded function.
let myPets = [ "πΆ", "π±", "π°" ]
let outdoorPets = myPets.filter(
pet => ( pet !== "π°" )
) ;
// outdoorPets is now [ "πΆ", "π±" ]
Returns a new array of the same size that transforms your original array.
let myPets = [ "πΆ", "π±", "π°" ]
let petsWithFood = myPets.map(
pet => return ( pet + "π²" )
) ;
// Or, with backticks:
let petsWithFood = myPets.map(
pet => return ( `${pet}π²` )
) ;
// petsWithFood is now [ "πΆπ²", "π±π²", "π°π²" ]
βΉοΈ The arrow function inside map doesn't require a return if it's on one line.
βΉοΈ map is very common in React Native!
Returns a single value from your embedded function.
let myPets = [ "πΆ", "π±", "π°" ]
let lovingPets = myPets.reduce(
( petsSoFar, pet ) => `${petsSoFar}β€οΈ${pet}`,
""
) ;
// lovingPets is now πΆβ€οΈπ±β€οΈπ°
βΉοΈ The arrow function inside reduce doesn't require a return if it's on one line.
βΉοΈ counter and pet are variables invented just for the loops and have no meaning outside.
βΉοΈ The second argument to reduce (in this case, an empty string) is the initial value before you start adding pets in the loop.
// Semicolons directly inside () will throw an error.
myDivs.forEach(
div => alert( div.id ) ; β
) ;
// Semicolons inside braces {} are OK.
myDivs.forEach(
div => {
alert( div.id ) ; β
div.id.style.display = "block" ; β
}
) ;
/