# Does Javascript let you default input params like Ruby does?

**URL:** <https://www.ruby-forum.com/t/does-javascript-let-you-default-input-params-like-ruby-does/98306>\
**Category:** Rails\
**Created:** [July 13, 2006, 7:19pm UTC](https://www.ruby-forum.com/t/does-javascript-let-you-default-input-params-like-ruby-does/98306 "2006-07-13T19:19:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 13, 2006, 7:19pm UTC](https://www.ruby-forum.com/t/does-javascript-let-you-default-input-params-like-ruby-does/98306/1 "2006-07-13T19:19:03Z")

</div>

Can you have a Javascript function with a param that’s defaulted if it  
isn’t present?

function X(a, b = false, c = ‘dog’) {  
}

X(5) calls X with a=5, b=false, c=‘dog’  
X(4, true) calls X with a=4, b=true, c=‘dog’  
X(3, true, ‘cat’) calls X with a=3, b=true, c=‘cat’

?

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 13, 2006, 7:57pm UTC](https://www.ruby-forum.com/t/does-javascript-let-you-default-input-params-like-ruby-does/98306/2 "2006-07-13T19:57:47Z")

</div>

I hope I am wrong, but I believe I remember hearing that this cannot be  
done like it is in RoR, PHP, etc. I remember seeing something like this:

\<%= button\_to\_function “Greet the world”, “hello()” %\>  
\<%= button\_to\_function “General Greeting”, “hello(‘there’)” %\>

This is kind of a lame work around, but it works. Hopefully there is a  
better solution.

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 13, 2006, 9:36pm UTC](https://www.ruby-forum.com/t/does-javascript-let-you-default-input-params-like-ruby-does/98306/3 "2006-07-13T21:36:14Z")

</div>

robert bradford wrote:

> I hope I am wrong, but I believe I remember hearing that this cannot be  
> done like it is in RoR, PHP, etc. I remember seeing something like this:
> 
> \<%= button\_to\_function “Greet the world”, “hello()” %\>  
> \<%= button\_to\_function “General Greeting”, “hello(‘there’)” %\>
> 
> This is kind of a lame work around, but it works. Hopefully there is a  
> better solution.

I probably shouldn’t have asked - I found out that you _can_ do this but  
not defaulting in the function definition.

You have to interrogate the built-in Javascript “arguments” array to see  
if there are extra arguments and then default them based on your needs,  
etc.

There is no explicit contract between the caller and the callee - I can  
pass as many arguments as I want to a Javascript function and the number  
of arguments that it receives can be less.

In this way, Javascript is even more dynamic than Ruby in it’s calling  
semantics since Ruby will throw a run time error if you call a function  
with the wrong number of parameters - as I’m sure all of us Railists  
know :)!

Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 13, 2006, 9:37pm UTC](https://www.ruby-forum.com/t/does-javascript-let-you-default-input-params-like-ruby-does/98306/4 "2006-07-13T21:37:06Z")

</div>

nope.

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 13, 2006, 10:17pm UTC](https://www.ruby-forum.com/t/does-javascript-let-you-default-input-params-like-ruby-does/98306/5 "2006-07-13T22:17:53Z")

</div>

On Thu, Jul 13, 2006, Wes G. wrote:

> Can you have a Javascript function with a param that’s defaulted if it  
> isn’t present?
> 
> function X(a, b = false, c = ‘dog’) {  
> }
> 
> X(5) calls X with a=5, b=false, c=‘dog’  
> X(4, true) calls X with a=4, b=true, c=‘dog’  
> X(3, true, ‘cat’) calls X with a=3, b=true, c=‘cat’

The syntax is not so clean, but I ran across this the other day which  
looks like what you want:

[http://earthcode.com/blog/2006/01/optional\_args.html](http://earthcode.com/blog/2006/01/optional_args.html)

Ben

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 13, 2006, 10:24pm UTC](https://www.ruby-forum.com/t/does-javascript-let-you-default-input-params-like-ruby-does/98306/6 "2006-07-13T22:24:36Z")

</div>

Wes G. wrote:

> ?  
> There is no language support for it but there are ways to emulate it  
> since Javascript does not require you to send all arguments. Eg.

function X(a, b, c) {  
if( arguments.length \< 3 ) c = ‘dog’  
if( arguments.length == 1 ) b = false

```
//... implementation

```

}

Some people will use the short-circuited “or” operator as follows:

function Y(m, n, o) {  
n = n || ‘foo’  
o = o || ‘bar’

```
//... implementation

```

}

This can be dangerous if a valid value for “n” or “o” can evaluate to  
false. In Ruby only “nil” and “false” evaluate to false but in  
Javascript the follow all evaluate to false:

undefined  
null  
0  
-0  
“” (empty string)  
NaN

An empty string and 0 are often valid values. Prototype has this fault  
in it’s implementation of the $F() function for input elements.

Another entirely different option is to use object literals to emulate  
optional named arguments. So:

function M(name, options) {  
defaults = {  
foo: ‘bar’,  
baz: ‘cat’  
}  
options.prototype = defaults  
//… implementation using “options” to access  
//… optional arguments  
}

You would call this like:

M(‘some\_val’, {  
foo: ‘dog’,  
baz: ‘car’  
});

If you remove an option it would get the default specified. So:

M(‘another\_val’, { foo: ‘dog’ });

Eric
