Calculating the sum of an array of numbers
We can reduce an array of numbers into a single value that represents the sum of its values, without needing a traditional for-loop.
Read full articleSometimes, the else
in an if
-else
is optional. When the if
-branch ends with a return
keyword, we don’t have to nest the alternative case in else
at all. We can place the code that would usually be under the else
-condition out to the same indentation as the if
branch.
const getWeekdayMood = isMonday => {
if (isMonday) {
// If the condition is `true`, the function returns a value and ends.
return "Yes, a new week!"
} else {
// This code only happens if the condition is `false`.
return "Time for another Monday soon!"
}
}
const getWeekdayMood = isMonday => {
if (isMonday) {
// Same deal: if this returns, the function ends. Nothing else happens.
return "Yes, a new week!"
}
// Anything here also only happens if the condition is `false`. We don’t
// need to nest our code in an `else` for that.
// Look ma, no `else`!
return "Time for another Monday soon!"
}
const getWeekdayMood = isMonday => {
if (isMonday) {
// If the condition is `true`, the function returns a value and ends.
return "Yes, a new week!"
} else {
// This code only happens if the condition is `false`.
return "Time for another Monday soon!"
}
}
const getWeekdayMood = isMonday => {
if (isMonday) {
// Same deal: if this returns, the function ends. Nothing else happens.
return "Yes, a new week!"
}
// Anything here also only happens if the condition is `false`. We don’t
// need to nest our code in an `else` for that.
// Look ma, no `else`!
return "Time for another Monday soon!"
}
const getWeekdayMood = isMonday => {
if (isMonday) {
// If the condition is `true`, the function returns a value and ends.
return "Yes, a new week!"
} else {
// This code only happens if the condition is `false`.
return "Time for another Monday soon!"
}
}
const getWeekdayMood = isMonday => {
if (isMonday) {
// Same deal: if this returns, the function ends. Nothing else happens.
return "Yes, a new week!"
}
// Anything here also only happens if the condition is `false`. We don’t
// need to nest our code in an `else` for that.
// Look ma, no `else`!
return "Time for another Monday soon!"
}
const getWeekdayMood = isMonday => {
if (isMonday) {
// If the condition is `true`, the function returns a value and ends.
return "Yes, a new week!"
} else {
// This code only happens if the condition is `false`.
return "Time for another Monday soon!"
}
}
const getWeekdayMood = isMonday => {
if (isMonday) {
// Same deal: if this returns, the function ends. Nothing else happens.
return "Yes, a new week!"
}
// Anything here also only happens if the condition is `false`. We don’t
// need to nest our code in an `else` for that.
// Look ma, no `else`!
return "Time for another Monday soon!"
}
We can reduce an array of numbers into a single value that represents the sum of its values, without needing a traditional for-loop.
Read full articleAs soon as a concatenation of conditions is clear, JavaScript stops the evaluation. It does not do work that does not change the result.
Read full articleInstead of removing elements from an array one by one, we can modify the array itself to clear it in one operation.
Read full articleArrays can contain the same value many times, while sets cannot. We can filter an array to unique values by using this difference.
Read full articleThere is no “array” type in JavaScript. To check if something is an array, we can use a helper on the Array prototype.
Read full articleTo remove the largest value from an array of numbers we first need to find that number and can then filter it out.
Read full article