Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Function Parameters in JavaScript

Function Parameters in JavaScript

? Function Parameters in JavaScript


What are Function Parameters?

  • Parameters are named variables in a function definition that receive values when the function is called.

  • They allow passing data into functions.


Types of Parameters

1. Regular Parameters

function greet(name) {  console.log("Hello, " + name);}greet("Alice");  // Output: Hello, Alice

2. Default Parameters (ES6+)

  • Parameters can have default values if no argument or undefined is passed.

function greet(name = "Guest") {  console.log("Hello, " + name);}greet();         // Hello, Guestgreet("Alice");  // Hello, Alice

3. Rest Parameters (ES6+)

  • Collects all remaining arguments into an array.

function sum(...numbers) {  return numbers.reduce((a, b) => a + b, 0);}console.log(sum(1, 2, 3)); // 6console.log(sum(4, 5));    // 9

4. Arguments Object (Old Style)

  • Inside non-arrow functions, you can access arguments — an array-like object of all passed arguments.

function showArgs() {  console.log(arguments);}showArgs(1, 2, 3); // Outputs: Arguments(3) [1, 2, 3, callee: ƒ, Symbol(Symbol.iterator): ƒ]
  • Not available in arrow functions.


Parameter vs Argument

TermMeaning
ParameterNamed variable in function definition
ArgumentActual value passed when calling function

Example Combining Features

function describe(name = "Unknown", age = 0, ...hobbies) {  console.log(`${name} is ${age} years old.`);  console.log("Hobbies:", hobbies.join(", "));}describe("Alice", 25, "Reading", "Hiking", "Coding");

Summary Table

FeatureDescriptionSyntax Example
Default ParametersDefault values if no argumentfunction f(a=1) {}
Rest ParametersCollects remaining args into arrayfunction f(...args) {}
Arguments ObjectArray-like of all argumentsarguments inside function

Want examples of destructuring parameters or how to handle optional parameters cleanly?

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql