Use default values in Javascript

This cool way to use default values for javascript function parameters was provided by Nils:

<script>

function(param1) {
param1 = param1 || "my default value";
}

</script>

Update: Fixed typo in the script above (| should have been ||), thanks Nathan for pointing that out! Took the opportunity to add an alternative way to implement below:
<script>

function(param1) {
param1 = typeof param1 !== 'undefined' ? param1 : "my default value";
}

</script>

Related posts:

Comments

comments powered by Disqus