Does make sense to check if the length of the array is not equal to 0 before of to do the loop?
var arr = [1,2,3];
if (arr.length !== 0) {
// do loop
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In the general case it doesn’t make sense. For a foreach, for, or while loop, the loop condition will prevent the loop executing anyway, so your check is redundant.
However, the only time it is important is if you are using a do…while loop. In this scenario, the loop executes, and then you check your loop-condition (when it is too late). It’s likely that your code would have thrown an exception inside the loop in this case, as demonstrated in the following code.