Besides function declarations and function expressions, JavaScript also has another very concise syntax for defining a function. These functions are called arrow functions.
Here is a comparison between a function declaration and an arrow function.
function addUpTwoNumbers(num1, num2) {
return num1 + num2;
}
// function keyword removed and => added
const addUpTwoNumbers = (num1, num2) => {
return num1 + num2;
};
If the function body contains only a return statement, like in the example above, the {} and the return keyword can be omitted.
If there is only one parameter, the parenthesis () can be omitted as well.
const addUpTwoNumbers = (num1, num2) => num1 + num2;
const square = num => num * num;
Arrow functions are often used to define short callback functions directly in the function call.
applyToSquare(number => number * number);
Arrays have built-in methods to analyse the contents of the array.
Most of these methods take a function that returns true or false as an argument.
Such a function is called a predicate.
The built-in methods are meant to be used instead of a for loop or the built-in forEach method:
Example of analysis using a for loop :
const numbers = [1, 'two', 3, 'four'];
for (var i = 0; i < numbers.length; i++) {
if (numbers[i] === 'two') {
return i;
}
}
// => 1
Example of analysis using a built-in method:
const numbers = [1, 'two', 3, 'four'];
numbers.indexOf('two');
// => 1
Some other helpful built-in methods that are available to analyze an array are shown below. See MDN for a full list of array methods.
includesThe includes(value) method determines whether an array includes a certain value.
It returns true when the value is included, false otherwise.
const numbers = [1, 'two', 3, 'four'];
numbers.includes(1);
// => true
numbers.includes('one');
// => false
everyThe every(predicate) method take a function which is a predicate.
It tests whether all elements in the array return true when passed to the predicate.
In other words: the methods tests that all its elements pass the test passed to the function call.
It returns true when every element passes the predicate test, false otherwise.
const numbers = [1, 3, 5, 7, 9];
numbers.every((num) => num % 2 !== 0);
// => true
someThe some(predicate) method is the same as the every method, but returns true if at least one item in the array passes the predicate test.
const numbers = [1, 3, 5, 7, 9];
numbers.some((num) => num % 2 !== 0);
// => true
findThe find(predicate) method returns the value of the first element in the array that passes the predicate test.
Where some() returns true when it passes, find() returns the actual value in the array.
The method returns undefined when none of the elements in the array pass the predicate test.
const numbers = [1, 3, 5, 7, 9];
numbers.find((num) => num < 5);
// => 1
findIndexThe findIndex(predicate) is the same as the find() method, but it returns the (first) index of the element that passes the predicate test instead of the value.
The method returns -1 when none of the elements in the array pass the predicate test.
const numbers = [1, 3, 5, 7, 9];
numbers.findIndex((num) => num > 7);
// => 4
numbers.findIndex((num) => num > 9);
// => -1
Elyse, magician-to-be, continues her training. She will be given several stacks of cards that she needs to perform her tricks. To make things a bit easier, she only uses the cards 1 to 10.
In this exercise, use built-in methods to analyse the contents of an array.
Elyse wants to know the position (index) of a card in the stack.
const card = 2;
getCardPosition([9, 7, 3, 2], card);
// => 3
Elyse wants to determine if a card is present in the stack -- in other words, if the stack contains a specific number.
const card = 3;
doesStackIncludeCard([2, 3, 4, 5], card);
// => true
Elyse wants to know if every card is even -- in other words, if each number in the stack is an even number.
isEachCardEven([2, 4, 6, 7]);
// => false
Elyse wants to know if there is an odd number in the stack.
doesStackIncludeOddCard([3, 2, 6, 4, 8]);
// => true
Elyse wants to know the value of the first card that is odd.
getFirstOddCard([4, 2, 8, 7, 9]);
// => 7
Elyse wants to know the position of the first card that is even.
getFirstEvenCardPosition([5, 2, 3, 1]);
// => 1
Sign up to Exercism to learn and master JavaScript with 37 concepts159 exercises, and real human mentoring, all for free.