Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I declare the same variable twice in different for loops in JavaScript? [duplicate]

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.

like image 873
Uri Avatar asked Dec 23 '12 10:12

Uri


People also ask

Can you declare the same variable twice in JavaScript?

In let and const you cannot declare the variable twice.

Can two for loops have the same variable?

Is it okay to create multiple for loops with the same variable name? Yes, it's OK and widespread practice to do so.

Can variables be defined twice?

int a; means both declaration and definition, but we know that defining a variable more than once is not allowed.

How many times can you declare a variable?

a variable/function can be declared any number of times but it can be defined only once.


1 Answers

Any use of var foo in a function will scope foo to that function. It doesn't matter where in the function this takes place as var declarations are hoisted.

Additional uses of var foo in the same function are syntactically legal but will have no effect as the variable is already scoped to that function.

Since it has no effect, there is a school of thought that recommends against it (and in favour of a single var function at the very top of a function to perform all the scoping) to avoid implying that there is significance to it (to maintainers who are not entirely comfortable with this feature of JavaScript). JSLint will alert you to this usage.

like image 165
Quentin Avatar answered Sep 20 '22 23:09

Quentin