Click here to Skip to main content
15,917,320 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi please some one help me regarding return type of function

function xyz()
{
document.getelementById("txtbox");
}


what does this function will return ??
Posted
Comments
Sergey Alexandrovich Kryukov 28-Dec-13 18:06pm    
Invalid question. Who told you that each function should return anything and if it does, return any defined type? I'll explain why...
—SA

1) Javascript function does not have to return anything. 2) Even if some Javascript function always returns something, the particular type of return may not be defined; generally, Javascript uses dynamic typing paradigm:
http://en.wikipedia.org/wiki/JavaScript#Dynamic[^].

This may need some additional explanation: "types are associated with values, not variables". Even the same very function, depending, say, on parameters passed, may return different things of different type of not return anything at all, which will cause assignment of undefined object. What "type", for example, will be returned by the function shown below? Try to determine it:
JavaScript
function whoKnowsWhat(input) {
   if (input != 3)
      return input;
}

someVariable = whoKnowsWhat(3); // what will be assigned?
someVariable = whoKnowsWhat("some string"); // will become a string
someVariable = whoKnowsWhat(12); // will become an integer

In this example, the function will return object of different types and assign them to the same variable. In first case, nothing is returned, so this means leaving the variable unassigned.

—SA
 
Share this answer
 
v5
Comments
Maciej Los 28-Dec-13 18:20pm    
Perfect!
Sergey Alexandrovich Kryukov 28-Dec-13 18:29pm    
Thank you, Maciej.
By the way, I'm back to Skype and not overly busy at the moment...
—SA
Maciej Los 28-Dec-13 18:37pm    
I can't talk right now, sorry. Can we connect tomorrow?
BTW: Now is 01:00 AM almost in Poland ;) I'm going to sleep. I'm to tired to discuss about anything ;)
Sergey Alexandrovich Kryukov 28-Dec-13 19:26pm    
Maybe, please look a my status...
—SA
Nothing.
Find out the reason for yourself here: Learn JavaScript[^]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 28-Dec-13 18:08pm    
Well, this is fair enough, but needs some further explanations to a beginner, as the problem is not so trivial. (I voted 4.)
Please see my answer.
—SA
Peter Leow 28-Dec-13 21:59pm    
Thanks for the advice, noted and +5!.
Sergey Alexandrovich Kryukov 28-Dec-13 22:01pm    
And thank you, Peter.
—SA
Maciej Los 28-Dec-13 18:19pm    
Another 4 from me!
Peter Leow 28-Dec-13 22:00pm    
Thanks for the support, a virtual 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900