Functions
Pyyhkäise näyttääksesi valikon
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!
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.
Kiitos palautteestasi!
Kysy tekoälyä
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme