Linked Questions

2689 votes
30 answers
1.5m views

Set a default parameter value for a JavaScript function

I would like a JavaScript function to have optional arguments which I set a default on, which get used if the value isn't defined (and ignored if the value is passed). In Ruby you can do it like this: ...
Tilendor's user avatar
  • 49k
383 votes
2 answers
588k views

How can I declare optional function parameters in JavaScript? [duplicate]

Can I declare default parameter like function myFunc( a, b=0) { // b is my optional parameter } in JavaScript?
Uttam Dutta's user avatar
  • 5,350
3 votes
4 answers
28k views

Javascript : optional parameters in function [duplicate]

Let's say I have this : function concatenate(a, b, c) { // Concatenate a, b, and c } How do I handle those calls ? x = concatenate(a) x = concatenate(a, b) x = concatenate(a, c) How can I ...
user avatar
3 votes
1 answer
4k views

Javascript: check if variable is false or if it doesn't exist at all [duplicate]

I want to write a function that uses argument value. If argument does not exist it will use value true by default. var check = function(truthValue){ var val = truthValue || true; console.log(...
sublime's user avatar
  • 4,201
2 votes
2 answers
315 views

javascript: define a variable in a function [duplicate]

In PHP I can do something like this: public myFunction($variable=0) { //Do some stuff } In Javascript, the following does not work: var myFunction = function(variable=0 ) { //do some stuff.....
HappyCoder's user avatar
  • 6,193
0 votes
2 answers
423 views

JS if not a variable [duplicate]

so I want to write a function that takes 3 parameters, 3rd one being optional. I want something to check that if optional does not exist then give it a default value. IS there a IF Not way of doing it?...
user1871528's user avatar
  • 1,785
0 votes
5 answers
177 views

how to add a default value to a javascript function variable? [duplicate]

Possible Duplicate: Is there a better way to do optional function parameters in Javascript? Default value for function parameter? I can I do this on javascript (jQuery) function function ...
aurel's user avatar
  • 3,162
-1 votes
2 answers
349 views

Write a JavaScript function that changes behavior based on optional arguments [duplicate]

I want to write a function that takes an optional argument and changes its behavior depending on whether or not that argument is provided. This is the first way I thought to do this: function foo(...
bobbyz's user avatar
  • 5,076
1 vote
2 answers
114 views

Do you have to give all parameters when firing a function in Javascript? [duplicate]

I'm building a little click game with HTML, css and Javascript and I don't think this question belongs in the Game Dev section but here. I've a a function to put a title and a text in a message box ...
Derk's user avatar
  • 42
0 votes
3 answers
73 views

Default vars in a method? [duplicate]

Here's my method: p.show = function(message, status, timer){ //do stuff }; Is there a way, so that if vars such as timer are not passed in, to have a default for them, e.g, true. In php I would ...
panthro's user avatar
  • 24.2k
0 votes
2 answers
71 views

In JS what does processMethod = processMethod || function(){}; do? [duplicate]

I am reading through some javascript code, and I have seen a lot of code that looks like this: processMethod = processMethod || function(){}; it's usually found inside a function. I believe it's a ...
user avatar
0 votes
2 answers
67 views

How to handle optional parameters in function [duplicate]

I have a function named test which accepts two parameters. If any pass any input to that function, it works perfect. But I have a scenario where input may or may not come. How to handle such scenario. ...
user4324324's user avatar
3373 votes
69 answers
2.1m views

How can I merge properties of two JavaScript objects?

I need to be able to merge two (very simple) JavaScript objects at runtime. For example I'd like to: var obj1 = { food: 'pizza', car: 'ford' } var obj2 = { animal: 'dog' } obj1.merge(obj2); //obj1 ...
598 votes
23 answers
914k views

How can you use optional parameters in C#?

Note: This question was asked at a time when C# did not yet support optional parameters (i.e. before C# 4). We're building a web API that's programmatically generated from a C# class. The class has ...
Kalid's user avatar
  • 22.8k
316 votes
12 answers
213k views

What does the construct x = x || y mean?

I am debugging some JavaScript and can't explain what this || does: function (title, msg) { var title = title || 'Error'; var msg = msg || 'Error on Request'; } Why is this guy using var title =...
opHasNoName's user avatar
  • 20.8k

15 30 50 per page