The length property returns the length (size) of an array:
const list = ["Banana", 78, "Apple", 56];
let size = list.length; //prints 4 to the screen
With functions you can reuse code
You can write code that can be used many times.
You can use the same code with different arguments, to produce different results.
A JavaScript function is a block of code designed to perform a particular task.
A JavaScript function is executed when "something" invokes it (calls it).
//Function to compute the product of 2 numbers
function myFunction(p1, p2) {
return p1 * p2;
}
console.log(myFunction(2, 4));
Regular function: can return anything;
always runs to completion after invocation
Generator function: returns a Generator object;
can be paused and resumed with the yield operator
Async function: returns a Promise; can be paused
and resumed with the await operator
Async generator function: returns an AsyncGenerator object;
both the await and yield operators can be used
if/else statements
switch statements
loops.
To create a function we can use a function declaration.
It looks like this:
function showMessage() {
alert( 'Hello everyone!' );
}
showMessage();
showMessage();