askscript
Oct 27th 2008, 10:51 pm
Hi i am a JavaScript learner at the beginner stage.
i am learning about functions now and do not really understand this part of the "return" value.
<html>
<head>
<title>Date Printer</title>
<script type = "text/javascript">
<!--
Function getNiceDate()
{
var now = new Date();
var the_month = now.getMonth() + 1;
var the_day = now.getDate();
var the_year = now.getYear();
var the_nice_date = the_month + "/" + the_day + "/" + the_year;
return the_nice_date;
}
-->
</script>
<body>
Hello! Today is
<script type = "text/javascript">
<!--
var today = getNiceDate();
document.write(today);
-->
</script>
</head>
</body>
</html>
In the book,
It explains that the "return" code tells javascript to exit the function and return the value of the_nice_date to whatever variable is waiting for it.
(outputs whatever value comes after return)
I do not understand what do they mean by return the value to whatever variable is waiting for it.
Does it mean that if i want to create a variable for the function as in this case var today = getNiceDate(); i need to always put this "return" code to it? why?
when will i need to use it and in what sort of situation?
i am learning about functions now and do not really understand this part of the "return" value.
<html>
<head>
<title>Date Printer</title>
<script type = "text/javascript">
<!--
Function getNiceDate()
{
var now = new Date();
var the_month = now.getMonth() + 1;
var the_day = now.getDate();
var the_year = now.getYear();
var the_nice_date = the_month + "/" + the_day + "/" + the_year;
return the_nice_date;
}
-->
</script>
<body>
Hello! Today is
<script type = "text/javascript">
<!--
var today = getNiceDate();
document.write(today);
-->
</script>
</head>
</body>
</html>
In the book,
It explains that the "return" code tells javascript to exit the function and return the value of the_nice_date to whatever variable is waiting for it.
(outputs whatever value comes after return)
I do not understand what do they mean by return the value to whatever variable is waiting for it.
Does it mean that if i want to create a variable for the function as in this case var today = getNiceDate(); i need to always put this "return" code to it? why?
when will i need to use it and in what sort of situation?