#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
JavaScript Fundamentals

Passing values

Passing values means giving data to a function when you call it, so the function can work with that data. The values you supply in the call are arguments, and the names listed in the function definition that receive them are parameters. This is how one function can handle many different inputs instead of always doing the same fixed thing.

Parameters receive arguments in order

Arguments are matched to parameters by position: the first argument goes to the first parameter, the second to the second, and so on. The names do not need to match anything in the calling code.

function formatPrice(amount, currency = 'INR', decimals = 2) {
  return `${currency} ${amount.toFixed(decimals)}`;
}

formatPrice(450);               // "INR 450.00"
formatPrice(450, 'USD');        // "USD 450.00"
formatPrice(450, undefined, 0); // "INR 450"

function total(...prices) {
  return prices.reduce((sum, p) => sum + p, 0);
}
total(100, 250, 75);            // 425

function createUser({ name, role = 'student' }) {
  return `${name} (${role})`;
}
createUser({ name: 'Kiran' });  // "Kiran (student)"

Missing and extra arguments

JavaScript does not complain if the number of arguments does not match. A parameter that receives no argument is undefined. Extra arguments are simply ignored by the named parameters, though they can still be collected.

Default parameters, like currency = 'INR' above, fill in when an argument is missing or explicitly undefined. Passing null does not trigger the default, because null counts as a real value.

Rest parameters and object parameters

A rest parameter, written with three dots like ...prices, gathers any number of arguments into a real array. It must be the last parameter. This replaces the old arguments object, which is array-like but lacks array methods and is not available in arrow functions.

When a function needs many options, positional arguments become hard to read: what does book(3, true, false, 'AC') mean? Passing one object and destructuring it, as createUser does, gives every value a name at the call site and lets callers skip any option in any order.

Primitives are copied, objects are shared

JavaScript always passes arguments by value, but for objects that value is a reference. The practical result:

  • If you pass a number or string and reassign the parameter inside the function, the caller's variable does not change.
  • If you pass an object or array and modify it inside the function, for example with list.push(), the caller sees the change, because both refer to the same object.
  • If you reassign an object parameter to a brand new object, the caller's variable is not affected; only the local name now points elsewhere.
function addTag(tags) {
  tags.push('new');       // changes the caller's array
  tags = ['replaced'];    // only changes the local name
}

const myTags = ['js'];
addTag(myTags);
console.log(myTags);      // ["js", "new"]

Mistakes to watch for

Swapping argument order is a quiet bug: formatPrice('USD', 450) passes a string as amount and throws when toFixed is called. Object parameters avoid this. Another trap is a function unexpectedly mutating an array you passed in, which is why many developers copy first with [...tags] when the original must stay untouched.

Takeaway

  • Arguments fill parameters by position; missing ones become undefined unless a default is set.
  • Use rest parameters for "any number of values" and an options object for many settings.
  • Changing an object passed in changes the caller's object too, so copy it when that is not wanted.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course