JS backend: wrap loop body in block, if needed, small refactoring