Forum Discussion

Haiyangwhu's avatar
Haiyangwhu
Contributor
11 years ago

Is it possible to set default value to JScript function parameters in Test Complete?

Hi all,



Is it possible to set default value to parameters in Jscript function in Test Complete?

Something like:



function CustomMethod(param1 = 'root', param2 = 100)

{



}



So that  when i can call CustomMethod(), it calls CustomMethod("root", 100) actually.



Thanks,

Ocean

3 Replies

  • Philip_Baird's avatar
    Philip_Baird
    Community Expert
    Hi Ocean, no, you can not do what you want. However, you can use the logical or operator as a type of null coalescing operator to assign a default value to a local var as such:

     


    function CustomMethod(param1, param2)


    {


        var a = param1 || 'root';


        var b = param2 || 100;


    }


     


     


    Regards,


    Phil Baird

  • jose_pita's avatar
    jose_pita
    Super Contributor
    This is what I do in this cases:



    function awesome(param1, param2)

    {

      if(param1 == undefined)

        param1 = "whatever"

      if(param2 == undefined)

        param2 = 0;

    }



    awesome();



    This way you don't need to worry about what you send in.

    If you need to use this function on a keyword just send undefined as the parameters, not String type.
    • Seretta's avatar
      Seretta
      Occasional Contributor

      thanks, that's just what I needed