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

bookFunctions

Svep för att visa menyn

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?

Vänligen välj det korrekta svaret

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 4

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Avsnitt 1. Kapitel 4
some-alt