2.js-functions.md
Quiz
~/ hackweb.dev
...
~/
/tutorials
/en/js/js-functions/edit
~ Contribute
Suggest a correction or improvement. The author reviews it before it goes live.
Loading...
en/tutorials/js/2js-functions
# JavaScript Functions Functions are reusable blocks of code. Define once, call anywhere. ## Declaring a function A function declaration names a block you can run later. ```js function greet(name) { console.log("Hello, " + name); } greet("Ada"); greet("Grace"); ``` ## Parameters and arguments Parameters are the inputs a function expects. Arguments are the values you pass in. ```js function add(a, b) { return a + b; } const total = add(3, 4); console.log(total); ``` ## Return values `return` sends a value back to the caller. Without it, a function returns `undefined`. ```js function square(n) { return n * n; } console.log(square(5)); ``` ## Arrow functions Arrow functions are a shorter syntax, common in modern JavaScript. ```js const double = (n) => n * 2; console.log(double(21)); ```
Submit suggestion
cancel