iteration-statement-for.js 1006 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // Copyright 2019 Google, LLC. All rights reserved.
  2. // This code is governed by the BSD license found in the LICENSE file.
  3. /*---
  4. esid: prod-OptionalExpression
  5. description: >
  6. optional chain in init/test/update of for statement
  7. info: |
  8. IterationStatement
  9. for (Expression; Expression; Expression) Statement
  10. features: [optional-chaining]
  11. ---*/
  12. // OptionalExpression in test.
  13. let count;
  14. const obj = {a: true};
  15. for (count = 0; obj?.a; count++) {
  16. if (count > 0) break;
  17. }
  18. assert.sameValue(count, 1);
  19. // OptionalExpression in init/test/update.
  20. let count2 = 0;
  21. const obj2 = undefined;
  22. for (obj?.a; obj2?.a; obj?.a) { count2++; }
  23. assert.sameValue(count2, 0);
  24. for (obj?.a; undefined?.a; obj?.a) { count2++; }
  25. assert.sameValue(count2, 0);
  26. // Short-circuiting
  27. let touched = 0;
  28. const obj3 = {
  29. get a() {
  30. count++;
  31. return undefined; // explicit for clarity
  32. }
  33. };
  34. for (count = 0; true; obj3?.a?.[touched++]) {
  35. if (count > 0) { break; }
  36. }
  37. assert.sameValue(count, 1);
  38. assert.sameValue(touched, 0);