Functions
Function Basics
A function is a reusable block of code. You define it once and call it whenever you need it.
Syntax:
function name(parameter1, parameter2) {
// code
return result;
}function greet(name) {
return "Hello, " + name + "!";
}
console.log(greet("Sara"));
console.log(greet("Bilal"));Output
Hello, Sara! Hello, Bilal!
- Parameters are the names in the definition (
name). - Arguments are the actual values you pass in (
"Sara"). returnsends a value back and ends the function. Without it, the function returnsundefined.