Linked Questions
84 questions linked to/from Set a default parameter value for a JavaScript function
864
votes
28
answers
513k
views
Is there a better way to do optional function parameters in JavaScript? [duplicate]
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
Default argument values in JavaScript functions [duplicate]
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
How do I set the default value for an optional argument in Javascript? [duplicate]
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'...
34
votes
9
answers
26k
views
Optional parameters in JavaScript [duplicate]
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 ...
10
votes
3
answers
11k
views
ES6 Default value parameter for callback [duplicate]
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
What is this communicating: my_var = my_var || 69 [duplicate]
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 ...
2
votes
2
answers
3k
views
How to pass an object as default argument to function in ecmascript? [duplicate]
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
450
views
pre-defined parameters [duplicate]
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 ...
0
votes
5
answers
417
views
Is JavaScript support default parameters in its functions? [duplicate]
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
Set default value on function when not passed expected values [duplicate]
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
Default value for parameter expect function [duplicate]
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
391
views
the javascript code works but I get an error [duplicate]
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
How can I make Javascript parameters default to something if not present? [duplicate]
I have this function:
this.checkButton = function (buttonName, element, expectedPresent, expectedEnabled) {
var enabledCheck = expectedEnabled ? "enabled" : "disabled";
it('Find a ' + ...
1
vote
3
answers
208
views
One-liner to call a function if it's defined, else call another function [duplicate]
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 ...
1
vote
1
answer
259
views
Creating default gridSize in javascript [duplicate]
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 ...