I have a habit of declaring every variable in a scope at the top block for code readability. I'm suspecting that nodes inside a function scope that are not added to the body of a document clutter up space, and reduce performance. What happens to them? And in general what happens to variables created inside a function scope? Are they destroyed after the function is executed? Do I need to worry about deallocating memory? Is it a good practice to declare variables at the top most block of a scope or is it better to only declare them at the spot when they are needed or when some condition is true? Does this help to improve run time performance?
Assume a function like this:
function myfunc() {
var someNode = document.createElement('div');
if(someCondition) { // add the node only if some condition is true
document.body.appendChild(someNode);
}
V.S a function like this:
function myfunc() {
if(someCondition) { // create and add the node only if some condition is true
var someNode = document.createElement('div');
document.body.appendChild(someNode);
}
}