Hi,
So I'm trying to write a function that not all parameters are declared. I can just declare what I need to and everything else "Defaults"
I'll confess I don't fully understand functions that have {} around the parameters. But from what I've seen this seems like the best way todo it. So I can type the name of the parameter I want to use and then not do the others... It doesn't seem to work like this..
function test({ param1, param2, param3 }) {
}
test({
param1: "test",
param2: 5,
param3: 2,
})
test({
param1: "test",
param3: 2,
})
The second invoking of the function doesn't work as not all the parameters are declared.
Ive tried to google things but I don't think I'm searching for the right things!
Thanks in advance!
Jack
- Dustin Harris @Dustin_Harris
Here's a quick and dirty example on how to assign default parameters:
function test({ param1 = "Test", param2 = 5, param3 = 2 } = {}) { log(param1 + param2 + param3) } test(); test({}); test({param1: "BOOM"}); test({param2: 1000});
The first two examples on what happens when you provide no parameters at all.
- In reply toJack_Green⬆:Chris Shaw @Chris_Shaw2024-06-11 15:13:07.476Z
Using
{}
around parameters is just a convenient way to pass a bunch of parameters into a function using an object.
You could have something like thisfunction test(param1, param2, param3, param4, param5, param6) {//code here}
but every param passed into that function must be declared in the correct order.
By passing params with a function you can pass in just what is needed and in any order and just de-structure them inside the function (see last example).There's no hard and fast rule but generally if you're passing more than three parameters it's easier to put them into an object (which is what you're doing in your test function above)
As for defaults you can declare them in the function like this:function test(firstName = "Chris",lastName = "Shaw"){ log (`${firstName} ${lastName}`) }
If you run
test()
it will logChris Shaw
Running
test("David")
it will logDavid Shaw
To override the last name only though you would have to dotest(undefined, "Smith")
which would outputChris Smith
An alternate way to declare defaults would be:
function test2(firstName, lastName) { if (!firstName) { firstName = "Chris" } if (!lastName) { lastName = "Shaw" } log(`${firstName} ${lastName}`) }
If you want to pass names as an object and declare defaults you can use:
function test3(nameObject) { //Destructure passed object let {firstName, lastName} = nameObject // Use defaults if neccessary if (!firstName) { firstName = "Chris" } if (!lastName) { lastName = "Shaw" } log(`${firstName} ${lastName}`) } test3({firstName:"Dan"}) // "Dan Shaw"
Although the canonical way of doing the above would be:
function test3(nameObject) { //Destructure passed object let { firstName, lastName } = nameObject // Use defaults if neccessary if (firstName === 'undefined') { firstName = "Chris" } if (lastName === 'undefined') { lastName = "Shaw" } log(`${firstName} ${lastName}`) }
If you want to learn more search for "javascript function parameter defaults"
- In reply toJack_Green⬆:Jack Green @Jack_Green
Thank you both.
I have a good play around with this tomorrow/when there isn't "real" work todo!