iteration-statement-for.js (1028B)
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 13 // OptionalExpression in test. 14 let count; 15 const obj = {a: true}; 16 for (count = 0; obj?.a; count++) { 17 if (count > 0) break; 18 } 19 assert.sameValue(count, 1); 20 21 // OptionalExpression in init/test/update. 22 let count2 = 0; 23 const obj2 = undefined; 24 25 for (obj?.a; obj2?.a; obj?.a) { count2++; } 26 assert.sameValue(count2, 0); 27 28 for (obj?.a; undefined?.a; obj?.a) { count2++; } 29 assert.sameValue(count2, 0); 30 31 // Short-circuiting 32 let touched = 0; 33 const obj3 = { 34 get a() { 35 count++; 36 return undefined; // explicit for clarity 37 } 38 }; 39 for (count = 0; true; obj3?.a?.[touched++]) { 40 if (count > 0) { break; } 41 } 42 assert.sameValue(count, 1); 43 assert.sameValue(touched, 0); 44 45 reportCompare(0, 0);