No internet connection
  1. Home
  2. How to

Functions without all parameters declared.

By Jack Green @Jack_Green
    2024-06-11 13:59:42.721Z

    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

    Solved in post #2, click to view
    • 3 replies
    1. Dustin Harris @Dustin_Harris
        2024-06-11 14:41:29.557Z

        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.

        Reply1 LikeSolution
        1. In reply toJack_Green:

          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 this

          function 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 log Chris Shaw

          Running test("David") it will log David Shaw
          To override the last name only though you would have to do test(undefined, "Smith") which would output Chris 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"

          1. In reply toJack_Green:
            Jack Green @Jack_Green
              2024-06-11 16:31:11.116Z

              Thank you both.

              I have a good play around with this tomorrow/when there isn't "real" work todo!