Yes, this is also called coding motion out of loops, and is one of Bentley's 27 rules for code tuning. Also, using local variables is much faster. Many of these refactorings are covered in Chapter 10, "Optimizing JavaScript for Execution Speed."
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
There should not be any Spelling Mistakes.
There should not be any Gramatical Errors.
Answers must not contain any bad words.
Answers should not be the repeat of same answer, already approved.