Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Functions | Section
JavaScript Essentials for React Native Development

bookFunctions

Swipe to show menu

A function in JavaScript is a reusable block of code that performs a specific task. You define a function using the function keyword, followed by a name, a list of parameters inside parentheses, and a block of code called the function body inside curly braces.

12345678
// Function declaration function greet(name) { return "Hello, " + name + "!"; } // Calling the function const message = greet("Sam"); console.log(message); // Output: Hello, Sam!
copy

The parameters are placeholders for values you provide when calling the function. In the example above, name is a parameter. The function body contains the code that runs when the function is called. Inside the body, you can use a return statement to send a value back to the place where the function was called. If you do not include a return statement, the function returns undefined by default.

When you call a function, you provide arguments, which are the actual values for the parameters. In the code sample, the function greet is called with the argument "Sam". The function returns the string "Hello, Sam!", which is stored in the variable message and then printed to the console.

question mark

What does a function do?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 4

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Section 1. Chapter 4
some-alt