Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What are JavaScript Function Parameters?
3.
Declaring a Function with Parameters
3.1.
JavaScript
3.2.
JavaScript
3.3.
JavaScript
4.
Function Parameters and Scope
4.1.
Multiple Parameters
4.2.
JavaScript
4.3.
Default Parameters
4.4.
JavaScript
5.
Frequently Asked Questions
5.1.
Can a function have parameters but never use them?
5.2.
Can we call a function with more arguments than there are parameters?
5.3.
Do all parameters need to have a default value?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

JavaScript Function Parameter

Roadmap to SDE career at Amazon
Speaker
Anubhav Sinha
SDE-2 @
25 Jun, 2024 @ 01:30 PM

Introduction

JavaScript functions are a fundamental building block of scripting on the web. They're powerful tools that perform specific tasks and can be reused throughout your code. This guide aims to simplify the understanding of JavaScript function parameters for beginners.

What are JavaScript Function Parameters?

Function parameters are inputs that we can pass into a function to influence its operation or outcome. When you declare a function, you can define it with parameters. These are essentially variables that the function will know about and can work with.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Declaring a Function with Parameters

Declaring a function with parameters in JavaScript follows a simple syntax. Here's an example:

  • JavaScript

JavaScript

function greet(name) {

  console.log("Hello, " + name);

}

 

In this function, 'name' is the parameter. When we call this function and pass a name, it uses that name in its operation.

  • JavaScript
  • JavaScript

JavaScript

function greet(name) {

  console.log("Hello, " + name);

}

JavaScript

greet("John");  // Output: Hello, John

Function Parameters and Scope

The scope of function parameters is limited to the function they are declared in. In other words, you can't access a function parameter outside that function.

Multiple Parameters

Functions can have more than one parameter. For instance, consider a function to calculate the area of a rectangle:

  • JavaScript

JavaScript

function calculateArea(length, width) {

  let area = length * width;

  console.log("The area of the rectangle is " + area);

}



calculateArea(5, 4);  // Output: The area of the rectangle is 20

 

In this example, the function calculateArea() has two parameters: length and width.

Default Parameters

JavaScript ES6 introduced default parameters. Default parameters allow you to assign a default value to the function parameter, which will be used if no argument is provided at function call:

  • JavaScript

JavaScript

function greet(name = "Stranger") {

  console.log("Hello, " + name);

}




greet();  // Output: Hello, Stranger

 

Here, if we call the function without any arguments, it will use "Stranger" as a default name.

Frequently Asked Questions

Can a function have parameters but never use them?

Yes, a function can declare parameters but never use them. It won't cause an error, but it's generally considered poor practice.

Can we call a function with more arguments than there are parameters?

Yes, in JavaScript, you can call a function with more arguments than there are parameters. The extra arguments are ignored.

Do all parameters need to have a default value?

No, it's not necessary to provide default values for all parameters. You can choose to provide default values for some, all, or none of your parameters.

Conclusion

Understanding function parameters in JavaScript is crucial to creating dynamic and reusable code. They allow us to write flexible functions that can perform differently based on the inputs they receive. From declaring multiple parameters to setting up defaults, function parameters open up a world of possibilities in our code. By understanding how they work and how to use them, we can take our JavaScript coding skills to the next level. Remember, JavaScript is a tool, and learning to use it effectively is the key to unlocking its full potential.

Previous article
Lodash _.matches() Method
Next article
Lodash _.extend() Method
Live masterclass