最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

how to initialize a parameter in javascript function when i define the function - Stack Overflow

programmeradmin6浏览0评论

i have a defined a function with a parameter in javascript, and i don't know how to initialize it's parameter, any one help me. the code is bellow

<br/>Enter a number:
<input type="text" id="txt1" name="text1">
<p>Click the button to see the result</p>
<button onclick="myFunction(5)">show result</button>
<br/>
<p id="demo"></p>
<script>
  function myFunction(param) {
    alert(param);
    var z = param;
    var y = document.getElementById("txt1").value;
    var x = +y + +z;
    document.getElementById("demo").innerHTML = x;
  }
</script>

the problem which i have is: if i send an argument to the function as <button onclick="myFunction(5)">show result</button> it works, but if i don't send any argument it is not working and the result is NaN. now i want to know how i can initialize the parameter wen i define the function? as we have in php function myFunction(param=0)

i have a defined a function with a parameter in javascript, and i don't know how to initialize it's parameter, any one help me. the code is bellow

<br/>Enter a number:
<input type="text" id="txt1" name="text1">
<p>Click the button to see the result</p>
<button onclick="myFunction(5)">show result</button>
<br/>
<p id="demo"></p>
<script>
  function myFunction(param) {
    alert(param);
    var z = param;
    var y = document.getElementById("txt1").value;
    var x = +y + +z;
    document.getElementById("demo").innerHTML = x;
  }
</script>

the problem which i have is: if i send an argument to the function as <button onclick="myFunction(5)">show result</button> it works, but if i don't send any argument it is not working and the result is NaN. now i want to know how i can initialize the parameter wen i define the function? as we have in php function myFunction(param=0)

Share Improve this question edited Jun 7, 2015 at 11:44 Ali asked Jun 7, 2015 at 10:31 AliAli 4071 gold badge8 silver badges26 bronze badges 1
  • possible duplicate of Set a default parameter value for a JavaScript function – Graeme Stuart Commented Jun 7, 2015 at 10:35
Add a ment  | 

5 Answers 5

Reset to default 5

Use param = param || 0; like

function myFunction(param) {
    param = param || 0;
    console.log(param);
    var z = param;
    var y = document.getElementById("txt1").value;
    var x = +y + +z;
    document.getElementById("demo").innerHTML = x;
  }

The above statement is saying set param to param if it is not falsey(undefined, null, false), otherwise set it to 0.

whenever you do not initialize a value to a parameter javascript will consider it as undefined, so you can reap the benefit of this issue and do it like:

function myFunction(param)
     {

       param = typeof param !== 'undefined' ? param : 0;

     }

hope it would be useful.

you'll have to do it like this:

function foo(a, b)
 {
   a = typeof a !== 'undefined' ? a : 42;
   ...
 }

All answers so far check the value of param to determine if it has been set or not. In most cases that works fine, but it doesn't cover cases in which you actually want to pass undefined to myFunction. Especially the often used pattern param = param || 0 will set param to 0 for all falsy values (0, false, "", null, undefined) that you pass to the function. So if you need to cover those cases as well and if you want to emulate the behavior of other languages as close as possible, you need to check arguments if param was actually set:

function myFunction(param) {
    if (arguments.length === 0) {
        param = 0;
    }
    console.log(param);
}

myFunction(0); //0
myFunction(1); //1
myFunction(""); //""
myFunction(null); //null
myFunction(undefined); //undefined
myFunction(false); //false
myFunction(); //0

Starting from ES2015

function myFunction(param1 = '', param2 = 0, param3 = false, param4 = {}, param5 = []) {}
发布评论

评论列表(0)

  1. 暂无评论