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.
includes
The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate. 1
const numbers = [1, 'two', 3, 'four'];
numbers.includes(1);
// => true
numbers.includes('one');
// => false
every
The every() method tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value. 2
const numbers = [1, 3, 5, 7, 9];
numbers.every((num) => num % 2 !== 0);
// => true
some
The some() method tests whether at least one element in the array passes the test implemented by the provided function. 3
const numbers = [1, 3, 5, 7, 9];
numbers.some((num) => num % 2 !== 0);
// => true
find
The find() method returns the value of the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned. 4
const numbers = [1, 3, 5, 7, 9];
numbers.find((num) => num < 5);
// => 1
findIndex
The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1, indicating that no element passed the test. 5
const numbers = [1, 3, 5, 7, 9];
numbers.findIndex((num) => num > 7);
// => 4
numbers.findIndex((num) => num > 9);
// => -1
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes β©
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every β©
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some β©
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find β©
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex β©
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 33 concepts, 149 exercises, and real human mentoring, all for free.