In JavaScript, functions are blocks of reusable code. They allow you to bundle functionality, make it more readable, and avoid repetition. Here’s a brief tutorial on writing an arrow function in JavaScript.
const functionName = (params) => {
// code to be executed
}
Example:
const greet = (name) => {
console.log("Hello, " + name + "!");
}
Tip: Functions often perform actions, so naming with a verb can make it clear what the function does. Examples include fetchData( ), calculateArea( ), or printReport( ).
To execute the function, you call or invoke it by using its name followed by parentheses.
Example:
greet('Alice'); // Outputs: Hello, Alice!`
Functions can process data input and output a value using the return keyword.
Example:
const addNums = (numA, numB) => {
return numA + numB
}
const total = addNums(2, 4);
console.log(total) // Expected value: 6
For more information on functions and how they are used in JS, check out the MDN docs.