Linked Questions

864 votes
28 answers
514k views

I've always handled optional parameters in JavaScript like this: function myFunc(requiredArg, optionalArg){ optionalArg = optionalArg || 'defaultValue'; // Do stuff } Is there a better way to do ...
231 votes
6 answers
219k views

Possible Duplicate: How do I make a default value for a parameter to a javascript function in PHP: function func($a = 10, $b = 20){ // if func() is called with no arguments $a will be 10 and $ ...
93 votes
3 answers
104k views

I am writing a Javascript function with an optional argument, and I want to assign the optional argument a default value. How can I assign it a default value? I thought it would be this, but it doesn'...
Royal Pinto's user avatar
  • 2,931
34 votes
9 answers
26k views

Question What is a good way of assigning a default value to an optional parameter? Background I'm playing around with optional parameters in JavaScript, and the idea of assigning a default value if ...
Richard's user avatar
  • 8,280
10 votes
3 answers
11k views

I have several functions with optional callback: let myFunc = (callback) => { callback = callback || (() => {}); // do something... callback(); } What is the best way to write the ...
18 votes
3 answers
1k views

I saw this in a Javascript example my_var = my_var || 69 I assume it means to check if my_var exists, if not set my_var to 69. Is this the case? Is there any documentation on this, it is very hard to ...
Dom Vinyard's user avatar
  • 2,088
2 votes
2 answers
3k views

i have an object that needs to be passed to the method. most often this object will be the same but sometimes should change to the value that the calling method provides consider i have the method ...
6 votes
4 answers
455 views

I want to be able to do this in JavaScript: function myFunction(one, two = 1) { // code } myFunction("foo", "2"); myFunction("bar"); I tried this and it doesn't work. I don't know how to call ...
anche's user avatar
  • 2,904
0 votes
5 answers
417 views

I want to make default parameter in javascript , so can i ? jm.toInt = function (num, base=10) { return parseInt(num,base); }
1 vote
1 answer
1k views

I want to make a handler for cases where the function is passed nothing, e.g. var v = Vector() as opposed to Vector(2,5,1) for example. var Vector3 = function(x, y, z) { this.x = x; this.y = y; this.z ...
0 votes
2 answers
1k views

I want to add default value for function parameter expect to receive a function. Look at the following example: /** * @param {function} callback * @param {String} name */ function example(...
0 votes
1 answer
393 views

I have created the following javascript code. This code is working fine but dreamweaver say, line (function load_unseen_notification(view = '')) something wrong. But what is the problem here code is ...
2 votes
2 answers
219 views

I have this function: this.checkButton = function (buttonName, element, expectedPresent, expectedEnabled) { var enabledCheck = expectedEnabled ? "enabled" : "disabled"; it('Find a ' + ...
user avatar
1 vote
3 answers
209 views

I want to call a callback function if one was provided by the user, or default to a default defaultCallback function. I've done it as follows: function defaultCallback(x) { console.log('default ...
Juicy's user avatar
  • 12.6k
1 vote
1 answer
259 views

I am writing a JavaScript function for setting the image which is required for my offline browser game. I am stuck on one situation that if by default the user does not passed the gridSize or the ...
user avatar

15 30 50 per page
1
2 3 4 5 6