Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende What Are Functions in JavaScript? | Functions in JavaScript
Introduction to JavaScript

book
What Are Functions in JavaScript?

Functions are specific objects that receive values, perform operations, and then return values. One of the most significant advantages of a function is its ability to be used multiple times, promoting code reusability.

Let's explore a simple function example:

function meeting(name) {
console.log("------ Meeting with " + name + " ------");
console.log("Hello, " + name + "!");
console.log("I'm glad to see you!");
console.log("But I need to go.");
console.log("Goodbye, " + name + "!");
console.log("See you again soon!");
}

meeting("Mary");
meeting("Kate");
meeting("Sean");
123456789101112
function meeting(name) { console.log("------ Meeting with " + name + " ------"); console.log("Hello, " + name + "!"); console.log("I'm glad to see you!"); console.log("But I need to go."); console.log("Goodbye, " + name + "!"); console.log("See you again soon!"); } meeting("Mary"); meeting("Kate"); meeting("Sean");
copy

In this example, the meeting function is defined and takes a name parameter. Inside the function is a code block containing various console.log statements. The function is called three times with different names as arguments.

The power of functions lies in their reusability. This function, written in just 8 lines of code, contains a code block of 6 lines. However, it can be called multiple times, eliminating the need to copy and paste those 6 lines of code.

Note

The example here aims to provide a basic understanding of functions. No detailed analysis is needed for now. In practice, functions are versatile and help developers organize and reuse code effectively.

question mark

What is the main benefit of using the calculateTotal function below?

function calculateTotal(price, quantity) {
console.log(price * quantity);
}

let totalOne = calculateTotal(12, 3);
let totalTwo = calculateTotal(5, 10);

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 6. Capítulo 1

Pregunte a AI

expand
ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

We use cookies to make your experience better!
some-alt