GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — JavaScript function - creating reusable code blocks JavaScript function keywordlast modified April 16, 2025
In this article we show how to create reusable code blocks using the
The function keyword
The Functions can accept parameters (inputs) and return values (outputs). They promote code reuse and make programs easier to understand and maintain. Functions are first-class objects in JavaScript.
JavaScript supports several function declaration styles: function declarations,
function expressions, arrow functions, and more. The Basic function declaration
The following example demonstrates the basic usage of the main.js
function greet() {
console.log('Hello, World!');
}
greet();
This is the simplest form of function declaration. The $ node main.js Hello, World! Function with parametersFunctions can accept parameters to make them more flexible and reusable. main.js
function greet(name) {
console.log(`Hello, ${name}!`);
}
greet('Alice');
greet('Bob');
This example shows a function that accepts a $ node main.js Hello, Alice! Hello, Bob! Function with return value
Functions can return values using the main.js
function square(number) {
return number * number;
}
const result = square(5);
console.log(result);
This function calculates the square of a number and returns the result.
The returned value can be assigned to a variable or used in expressions.
Functions without a return statement return $ node main.js 25 Function expressionFunctions can be defined using expressions, where the function is assigned to a variable. main.js
const multiply = function(a, b) {
return a * b;
};
console.log(multiply(3, 4));
This is a function expression. The function is anonymous (has no name) and
is assigned to the $ node main.js 12 Function hoistingFunction declarations are hoisted, meaning they can be called before they are defined in the code. main.js
console.log(add(2, 3));
function add(a, b) {
return a + b;
}
This works because function declarations are moved to the top of their scope during compilation. Note that function expressions are not hoisted in the same way. $ node main.js 5 Default parametersJavaScript supports default parameters for functions. main.js
function greet(name = 'Guest') {
console.log(`Hello, ${name}!`);
}
greet();
greet('Alice');
Default parameters provide fallback values when arguments are not provided. This makes functions more flexible and reduces the need for parameter validation code. $ node main.js Hello, Guest! Hello, Alice! Rest parametersFunctions can accept an indefinite number of arguments using rest parameters. main.js
function sum(...numbers) {
return numbers.reduce((total, num) => total + num, 0);
}
console.log(sum(1, 2, 3));
console.log(sum(1, 2, 3, 4, 5));
Rest parameters collect all remaining arguments into an array. This is useful for functions that need to handle variable numbers of arguments. The example shows a function that sums any number of values. $ node main.js 6 15 SourceIn this article we have demonstrated how to use the function keyword to create reusable code blocks in JavaScript. AuthorMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |