Possible Duplicate:
JavaScript Variable Scope
I have a JavaScript function for HTML select options for days:
// Show and hide days according to the selected year and month.
function show_and_hide_days(fp_form) {
var select_year= $(fp_form).find("select.value_year");
var select_month= $(fp_form).find("select.value_month");
var select_day= $(fp_form).find("select.value_day");
var selected_year= $.parse_int($(select_year).val());
var selected_month= $.parse_int($(select_month).val());
var selected_day= $.parse_int($(select_day).val());
var days_in_month= new Date(selected_year, selected_month, 0).getDate();
// If the number of days in the selected month is less than 28, change it to 31.
if (!(days_in_month >= 28))
{
days_in_month= 31;
}
// If the selected day is bigger than the number of days in the selected month, reduce it to the last day in this month.
if (selected_day > days_in_month)
{
selected_day= days_in_month;
}
// Remove days 29 to 31, then append days 29 to days_in_month.
for (var day= 31; day >= 29; day--)
{
$(select_day).find("option[value='" + day + "']").remove();
}
for (var day= 29; day <= days_in_month; day++)
{
$(select_day).append("<option value=\"" + day + "\">" + day + "</option>");
}
// Restore the selected day.
$(select_day).val(selected_day);
}
My question is - can I declare "var day" twice in two different for loops and what is the scope of this variable? Is it legal and what happens if I declare the same variable twice in the same function? (inside for loops or outside for loops)? For example, what happens if I declare one of the variables again with "var"?
If I don't use "var" at all before variable day in for loops, what will happen?
Thanks, Uri.
P.S. $.parse_int is a jQuery plugin which calls parseInt with radix 10 if not specified.