Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
JavaScript Function length property
Next article icon

JavaScript Function Parameters

Last Updated : 17 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Function parameters are variables defined in the function declaration that receive values (arguments) when the function is called. They play a key role in making functions reusable and dynamic.

  • Values are assigned to parameters in the order they are passed.
  • You can assign default values to parameters if no arguments are provided.
  • Allows capturing an indefinite number of arguments into an array.
  • Primitive types are passed by value, whereas objects are passed by reference.
JavaScript
function greet(name) {     return `Hello, ${name}!`; } console.log(greet("Meeta"));  

Output
Hello, Meeta! 
  • Parameter: name in the function definition.
  • Argument: "Meeta" passed when calling the function.

Types of Parameters in JavaScript

1. Required Parameters

These are the basic parameters expected by the function. If not provided, they will be undefined.

JavaScript
function add(a, b) {     return a + b; } console.log(add(5, 3));  console.log(add(5));  

Output
8 NaN 

2. Default Parameters

Introduced in ES6, default parameters allow you to assign a default value to a parameter if no argument is passed or if the argument is undefined.

JavaScript
function mul(a, b = 1) {     return a * b; } console.log(mul(5));    console.log(mul(5, 2)); 

Output
5 10 

3. Rest Parameters

Rest parameters allow a function to accept an indefinite number of arguments as an array. Use the ... syntax to capture all additional arguments.

JavaScript
function sum(...numbers) {     return numbers.reduce((total, num) => total + num, 0); } console.log(sum(1, 2, 3, 4));  

Output
10 

4. Destructured Parameters

You can destructure arrays or objects passed as arguments into individual variables.

JavaScript
function displayUser({ name, age }) {     return `${name} is ${age} years old.`; } const user = { name: "Meeta", age: 25 }; console.log(displayUser(user));  

Output
Meeta is 25 years old. 

5. Passing Functions as Parameters (Higher-Order Functions)

Functions in JavaScript can accept other functions as parameters, making it easy to create reusable code.

JavaScript
function executeTask(task, callback) {     console.log(`Task: ${task}`);     callback(); } executeTask("Clean the room", () => {     console.log("Task Completed!"); }); 

Output
Task: Clean the room Task Completed! 

Next Article
JavaScript Function length property

R

rathbhupendra
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • javascript-functions

Similar Reads

  • JavaScript Rest parameter
    The JavaScript Rest parameter allows a function to accept an indefinite number of arguments as an array. It is represented by three dots (...) followed by the parameter name and must be the last parameter in the function, enabling flexible and dynamic argument handling. Syntax//... is the rest param
    4 min read
  • JavaScript Function length property
    The Javascript Function.length property of the function object in Javascript is used to return the number of parameters required by a function. Syntax: function.length Parameters: This method requires no parameters. Return: Return type is number. A few examples are given below for a better understan
    2 min read
  • Variadic Functions in JavaScript
    Variadic functions are functions that can accept any number of arguments. In variadic functions no predefined number of function arguments is present that's why a function can take any number of arguments. Syntax:function NameOfFun( x1, x2, x3, ... ){ // function body}Below are the approaches by usi
    2 min read
  • Functional programming in JavaScript
    Functions are the most important part of functional programming (especially in JavaScript). Functions are the single source that helps developers to perform functional programming. Generally abbreviated as FP which revolves around functions and it is how we use functions that makes our code function
    7 min read
  • JavaScript Passing parameters to a callback function
    Callback FunctionPassing a function to another function or passing a function inside another function is known as a Callback Function. In other words, a callback is an already-defined function that is passed as an argument to the other code Syntax:function geekOne(z) { alert(z); }function geekTwo(a,
    2 min read
  • Functions in JavaScript
    Functions in JavaScript are reusable blocks of code designed to perform specific tasks. They allow you to organize, reuse, and modularize code. It can take inputs, perform actions, and return outputs. [GFGTABS] JavaScript function sum(x, y) { return x + y; } console.log(sum(6, 9)); [/GFGTABS]Output1
    5 min read
  • Argument vs Parameter in Java
    Argument An argument is a value passed to a function when the function is called. Whenever any function is called during the execution of the program there are some values passed with the function. These values are called arguments. An argument when passed with a function replaces with those variabl
    2 min read
  • JavaScript Function Complete Reference
    A JavaScript function is a set of statements that takes inputs, performs specific computations, and produces outputs. Essentially, a function performs tasks or computations and then returns the result to the user. Syntax: function functionName(Parameter1, Parameter2, ..) { // Function body}Example:
    3 min read
  • JavaScript Function Call
    The call() method is a predefined JavaScript method. It can be used to invoke (call) a method with an owner object as an argument (parameter). This allows borrowing methods from other objects, executing them within a different context, overriding the default value, and passing arguments. Syntax: cal
    2 min read
  • JavaScript Function.prototype.call() Method
    The call() method allows function calls belonging to one object to be assigned and it is called for a different object. It provides a new value of this to the function. The call() method allows you to write a method once and allows it for inheritance in another object, without rewriting the method f
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences