How best to determine if an argument is not sent to the JavaScript function

后端 未结 13 704
猫巷女王i
猫巷女王i 2020-11-28 00:40

I have now seen 2 methods for determining if an argument has been passed to a JavaScript function. I\'m wondering if one method is better than the other or if one is just ba

13条回答
  •  日久生厌
    2020-11-28 01:32

    In ES6 (ES2015) you can use Default parameters

    function Test(arg1 = 'Hello', arg2 = 'World!'){
      alert(arg1 + ' ' +arg2);
    }
    
    Test('Hello', 'World!'); // Hello World!
    Test('Hello'); // Hello World!
    Test(); // Hello World!

提交回复
热议问题