Delete a "definition" | Sololearn: Learn to code for FREE!


Delete a "definition"

If we have, for example, this code: function i(){ .... return "text";} var j=.. ; if(j==2) for (var i=0;i<....;i++) {....} document.write(i()); it will give an error message ("i" is not a function) because of the "i" inside the "for" loop. I don't want to modify the name of the function or the variable inside "for", I need to delete "i" which is inside "for" after finishing the loop (If the condition is met). another expression, how can I fix the error without changing names? is there a method? Or am I forced to modify every "i" inside the function or the loop?

3/6/2022 10:35:44 AM

Eyad Al-Ahmar

5 Answers

New Answer


You can use "let" keyword in for loop, so replace var with let for (let i=0;i<....,i++) {..}


More about let


In first code you type in question, if you change var to let, it will work, because you call function outside loop But here it wont because you call i function inside loop where something with same name exist. You need to change name of i inside loop or name of your function, if you plan to use this inside loop. This is how js works. It first check does this variable/function exist in current scope, in your case for loop, if exist it will try to run this code, if dont exist it will look to higher scope like function or global. Also it is good practice to name function with some name what explain purpose of function.


PanicS Problem solved! Thank you very much.


Ipang who asked that, not me, thanks anyway.