473,327 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Default value for functrion argument

Hi,

Is it possible to give a default value to a function argument:

I am trying to do something like this:

function my_func(arg1,arg2="")
{
}


Is it possible to do this in javascript???

Thanks in advance
May 22 '07 #1
4 1338
dmjpro
2,476 2GB
i think it is not possible..

but i m not sure...
did u test it???

what result u got???

kind regards.
dmjpro.
May 22 '07 #2
pbmods
5,821 Expert 4TB
Is it possible to give a default value to a function argument:
Nope. But here's what you can do:

Expand|Select|Wrap|Line Numbers
  1. function doSomething(parameter1, parameter2) {
  2.     //  Init parameters
  3.     if(parameter1 == undefined)
  4.         parameter1 = 5;
  5.  
  6.     if(parameter2 == undefined)
  7.         parameter2 = 10;
  8.  
  9.     return parameter1 * parameter2;
  10. }
  11.  
May 23 '07 #3
dmjpro
2,476 2GB
yes pbmods u r right.

kind regards.
dmjpro.
May 23 '07 #4
gits
5,390 Expert Mod 4TB
Nope. But here's what you can do:

Expand|Select|Wrap|Line Numbers
  1. function doSomething(parameter1, parameter2) {
  2.     //  Init parameters
  3.     if(parameter1 == undefined)
  4.         parameter1 = 5;
  5.  
  6.     if(parameter2 == undefined)
  7.         parameter2 = 10;
  8.  
  9.     return parameter1 * parameter2;
  10. }
  11.  
yep ... thats right ... but i want to mention that it should be better to replace the condition with:

Expand|Select|Wrap|Line Numbers
  1. // example: condition to replace if(parameter1 == undefined) with:
  2. if (typeof parameter1 == 'undefined') {
  3.     parameter1 = 'whatever';
  4. }
  5.  
it's due to the fact, that my ie throws an error until it seems not to recognize undefined as not to be a variable ...

kind regards ...
May 23 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Frank Bechmann | last post by:
Eventually most of you will not learn much from this because it's just another event in the 'default argument value gotcha' series, but because it cost me some hours yesterday to spot this 'error'...
5
by: Dave Vandervies | last post by:
If I feed this to g++: -------- int foo(int i=42); int foo(int i=42) { return i; } -------- It says (with -W -Wall -ansi -pedantic):
4
by: jagrat | last post by:
Hi all, I have a function declaration as: func(int somex = 0, int somey = 0, int* someval = NULL, vector<int> v_Val) compiler gives me error that last argument is without its default...
11
by: puzzlecracker | last post by:
not sure of the stardard: which one is right, if both which one is preferable? class FOO{ void foo(const string &, const string &username="true"); ..... } void FOO::foo(const string &...
3
by: Capstar | last post by:
Hi NG, I am trying to get the attached piece of code to work, but I can't figure out what I'm doing wrong. To me it seems that when I don't pass an argument to x::do_something, it should use the...
4
by: aling | last post by:
What's the rule of default argument of function in C++? I found that the default argument of function could not necessary be a constant value. Is it true? Previously I thought that the default...
44
by: gregory.petrosyan | last post by:
Hello everybody! I have little problem: class A: def __init__(self, n): self.data = n def f(self, x = ????) print x All I want is to make self.data the default argument for self.f(). (I
50
by: LaundroMat | last post by:
Suppose I have this function: def f(var=1): return var*2 What value do I have to pass to f() if I want it to evaluate var to 1? I know that f() will return 2, but what if I absolutely want to...
6
by: Igor V. Rafienko | last post by:
Hi, I was wondering whether it was possible to find out which parameter value is being used: the default argument or the user-supplied one. That is: def foo(x, y="bar"): # how to figure...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.