Understanding What is an Argument in JavaScript Function?

Wondering what is an argument in JavaScript Function?

Learn about arguments in JavaScript functions and how to use them effectively.

This article explains what arguments are, how to define and pass them to functions, and how to access them within a function.

Enhance your JavaScript skills with this comprehensive guide. Read on to discover everything you need to know about arguments in JavaScript functions.

What is an argument in JavaScript?

An argument in JavaScript, is a value that is passed to a function when it is called.

When you define a function, you can specify one or more parameters that the function expects to receive when it is called.

These parameters are like placeholders for the values that will be passed to the function when it is called. When a function is called, the values that are passed to it are known as arguments

Consider the following example:

function add(a, b) {
return a + b;
}

a and b are the parameters of the add function. When you call the add function, you pass two arguments to it, like this:

let result = add(50, 20);

50 and 20 are the arguments that are passed to the add function. The add function takes these arguments and uses them in its calculation.

Here’s the complete code:

function add(a, b) {
  return a + b;
}

let result = add(50, 20);
console.log(result);

Output:

70

JavaScript also provides an arguments object that is available within all non-arrow functions.

When you call a function, you can pass values to it, which are known as arguments.

Inside the function, there is a special object called the arguments object that behaves like an array and holds the values of the arguments that were passed to the function.

Here’s an example:

function sampleFunction() {
  let sum = 0;
  for (let i = 0; i < arguments.length; i++) {
    sum += arguments[i];
  }
  return sum;
}

let result = sampleFunction(10, 20, 30, 40, 50);
console.log(result); 

The sampleFunction function uses the arguments object to access the arguments that were passed to it when it was called.

The function calculates the sum of all the arguments and returns the result. When we call the sampleFunction function with the arguments 10, 20, 30, 40, and 50 it returns the following, which is the sum of these arguments.

Output:

150

Passing Arguments to JavaScript Functions

To pass arguments to a JavaScript function, you simply include the required values within the parentheses when calling the function.

These values can be literals, variables, expressions, or even other functions.

function calculateSum(x, y) {
    return x + y;
}

// Passing literals as arguments
const result = calculateSum(50, 70);
console.log(result); // Output 120

// Passing variables as arguments
const num1 = 100;
const num2 = 200;
const total = calculateSum(num1, num2);
console.log(total); // Output 300

This code defines a function called calculateSum that takes two arguments, x and y, and returns their sum.

The code then shows two examples of how to call this function and pass arguments to it.

In the first example, the literals 50 and 70 are passed as arguments to the calculateSum function.

The function returns the sum of these values, which is 120, and assigns it to the result variable.

The value of the result variable is then logged to the console using the console.log method.

In the second example, the values of the num1 and num2 variables are passed as arguments to the calculateSum function.

The function returns the sum of these values, which is 300, and assigns it to the total variable.

Output:

120
300

How to use arguments in a function?

To use arguments in a function, you need to define the function with parameters that will receive the values of the arguments passed to the function when it is called.

The arguments are passed to the function in the order in which they appear in the function call.

Here’s an example of how to define a function with arguments and how to use them within the function:

function sampleFunction(x, y) {
// Use the values of the arguments 'x' and 'y' within the function
console.log(x + y);
}

// Call the function and pass values for the arguments
sampleFunction(10, 20);

As you can see in our example code, the sampleFunction function is defined with two parameters: x and y.

These parameters will receive the values of the arguments passed to the function when it is called.

In this case, when we call sampleFunction(10, 20), the value 10 is assigned to parameter x and the value 20 is assigned to parameter y.

Within the function, you can use these argument values to perform calculations or other operations. In this example, we use the values of x and y to calculate their sum and log it to the console.

Conclusion

In conclusion, we have discussed what is arguments in JavaScript functions.

We thoroughly explain that arguments are values passed to functions when called and outline how to define, pass, and access them within a function.

We are hoping that this article provides you with enough information that helps you understand what is an argument in JavaScript.

If you want to dive into more JavaScript topics, check out the following articles:

Thank you for reading itsourcecoders 😊.

Frequently Asked Questions

Is JavaScript still worth learning in 2026?
Yes. JavaScript runs on 98% of websites for the front-end, dominates the back-end via Node.js, powers mobile apps through React Native, builds desktop tools through Electron, and is the scripting layer for most AI tooling (LangChain.js, OpenAI SDK, Vercel AI). Whether you target web, mobile, AI, or full-stack capstones, JavaScript is the broadest single language you can learn.
What is the difference between var, let, and const?
var is function-scoped, hoisted to the top of its scope, and can be redeclared, which leads to bugs in modern code. let is block-scoped (only visible inside the nearest {}) and can be reassigned. const is block-scoped and cannot be reassigned, although object contents can still mutate. Default to const for everything, switch to let only when you actually need to reassign, and avoid var in any code written after 2017.
Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds useful features like top-level await, private class fields with the # prefix, and the .at() array method. If you are writing for older browsers (IE11 or older Android WebViews), transpile down with Babel or use a build tool like Vite, esbuild, or webpack.
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard, free, with built-in IntelliSense, debugger, terminal, Git, and a huge extension marketplace (ESLint, Prettier, GitHub Copilot, Tailwind). Install the JavaScript and TypeScript Nightly extension for the latest language features. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, the Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
How do I run JavaScript locally vs in the browser?
In the browser: open DevTools with F12 (or right-click then Inspect), go to the Console tab, type or paste your code, press Enter. For HTML pages, add a script tag pointing to your .js file. Locally with Node.js: download Node from nodejs.org (LTS version), then run node script.js in your terminal from the file folder. Use the same Node setup for backend capstones, API integrations, and scripts that do not need a browser.
What can I build with JavaScript for my BSIT capstone?
Common BSIT capstones in JavaScript: full-stack web apps using React or Vue on the front-end with Node.js and Express on the back-end (MongoDB or MySQL for the database), real-time chat or notification systems using Socket.io, single-page dashboards with Chart.js or D3.js, cross-platform mobile apps with React Native, AI-powered chatbots using OpenAI SDK and LangChain.js, and Chrome extensions for productivity tools. Add Tailwind CSS for the UI and Vercel or Netlify for free deployment.
Caren Bautista

Technical Writer at PIES IT Solution

Responsible for crafting clear, well-structured, and beginner-friendly content across the platform. Handles the writing, proofreading, and editorial review of tutorials, guides, and documentation to ensure every article is accurate, readable, and easy to follow.

Expertise: Technical Writing · Content Creation · Documentation · Editorial Writing · JavaScript · TypeScript · Python · Python Errors · HTTP Errors · MS Excel  · View all posts by Caren Bautista →

Leave a Comment