Hobin C.
Hobin C.

Reputation: 761

How to pass data to function by the name of the parameters?

I am new to JavaSscript. Basically, I do not want to pass my data by the position of those function parameters. This is inconvenient when there are 10 or more parameters.

I want to pass it by the parameter name defined in the function. However, there is a side-effect when I use this statement: my_func(param01=1). In my view, the usage of param01=1 inside the function call, only represents that 1 is passed to the parameter param01 and no effect happens outside the function call.

This is my code:

function my_func(param01) {
    console.log('param01 inside my_func: ', param01)
}

param01 = 3
my_func(param01=1)
console.log('param01 outside my_func: ', param01)
// the result is 1 instead of 3.

My question is: What is the right way to pass data to function by the name of the function parameter?

Upvotes: 2

Views: 2078

Answers (5)

Krisztián Balla
Krisztián Balla

Reputation: 20381

I think the main problem here is a misunderstanding as to how parameters work in JavaScript. It looks like you come from a language where this statement:

my_func(param01=1)

Would pass a parameter called param0 with the value 1 to the function.

But in JavaScript this is what happens:

  1. param01 is a variable outside of your function and you assign 1 to it
  2. The result of that assignment expression is 1 so 1 is also passed the the function as a parameter

This explains why param01 is also 1 outside of your function.

In JavaScript you don't specify the parameter names when calling your function. So this works just the same:

my_func(1)

If you want to provide more parameters you have multiple options:

  1. You simply separate them with a comma: my_func(1, 2, 3, 4, 5, 6)

Your function declaration must then look like this:

function my_func(param1, param2, param3, param4, param5, param6)

Or you could use the rest parameter syntax:

function my_func(...params)

This way you can pass as many arguments as you like and they will all be in the params array. You can then access them like this: param[0] for the first parameter, param[1] for the second and so on.

  1. You can create an array with your values and pass the array as a parameter:
const parameters = [1, 2, 3, 4, 5 ,6];
my_func(parameters)

Your function declaration must then look like this:

function my_func(params)

As a side note on clean coding: Your function should have as few parameters as possible. If you have a lot of parameters this usually indicates that the function is doing too many things, thence violates the single responsibility pattern and should be split up into smaller functions.

Upvotes: 3

brk
brk

Reputation: 50326

Use destructuring assignment, In this case the function may accept 10 parameters but you don't need to order the parameters because the value inside the function will be retrieved using key name. Also the parameters can be in any order while calling the function.

function my_func({
  param01,
  param2,
  param3
}) {
  console.log('param01 inside my_func: ', param01, param3)
}
param01 = 0
my_func({
  param01: 1,
  param3: 4
})
console.log('param01 outside my_func: ', param01)

Upvotes: 3

cole
cole

Reputation: 13

You need to define every function parameters like what Jenny and brk did in the comments above.

Jenny: my_func(1, 2, 3, 4, 5, 6);

blk: my_func({ param01: 1, param3: 4 })

You cannot pass 10 or more parameters by the parameter name defined in the function if you don't teach the computer what the 10 or more parameters are.

Please clarify if I misunderstood your question.

Upvotes: 0

Mr. Polywhirl
Mr. Polywhirl

Reputation: 48693

If you want 0 on the outside, but 1 on the inside, you are gonna have a bad time.

The zero you pass in is perfectly acceptable. If you want zero to be a no-no... you can use default operator || which evaluates values for truthiness left-to-right and short-circuits.

function my_func(param01) {
  param01 = param01 || 1; // internal = 0 || 1 = 1
  console.log('param01 inside my_func: ', param01) // >>> 1
}

param01 = 0 // global = 0

my_func(param01) // pass in the global 0, which IS VALID

console.log('param01 outside my_func:', param01) // >>> 0

Alternatively, you can pass in an object.

function my_func(params={}) {
  console.log('param01 inside my_func: ', params.param01) // >>> 1
}

param01 = 0 // global = 0

my_func({ param01 : 1 }) // pass in a 1

console.log('param01 outside my_func:', param01) // >>> 0

Upvotes: 3

Elman Huseynov
Elman Huseynov

Reputation: 1147

Param name inside function declaring can be random, while the order is important. But you can use default parameter value in js, to set them by default:

function my_func(param01 = 1) {
    console.log('param01 inside my_func: ', param01)
}

Upvotes: 1

Related Questions