[{"data":1,"prerenderedAt":4115},["ShallowReactive",2],{"page-\u002Fjs\u002F04-control-flow":3},{"id":4,"title":5,"body":6,"description":35,"extension":4109,"meta":4110,"navigation":105,"path":4111,"seo":4112,"stem":4113,"__hash__":4114},"content\u002Fjs\u002F04-control-flow.md","04 — Control Flow",{"type":7,"value":8,"toc":4068},"minimark",[9,13,248,556,769,1032,1262,1508,1683,1850,2077,2277,2483,2631,2801,2965,3159,3354,3506,3693,3874,4064],[10,11,5],"h1",{"id":12},"_04-control-flow",[14,15,17,27,92,168],"question-wrapper",{"language":16},"javascript",[18,19,21,22,26],"h3",{"id":20},"q1-which-of-these-values-is-falsy-in-a-boolean-context-eg-inside-an-if","Q1. Which of these values is falsy in a boolean context (e.g., inside an ",[23,24,25],"code",{},"if",")?",[28,29,30],"code-wrapper",{"language":16},[31,32,36],"pre",{"className":33,"code":34,"language":16,"meta":35,"style":35},"language-javascript shiki shiki-themes github-light github-dark","const values = [\"\", 0, \"0\", [], {}, null, undefined, NaN];\n","",[23,37,38],{"__ignoreMap":35},[39,40,43,47,51,54,58,62,65,68,70,73,76,79,81,84,86,89],"span",{"class":41,"line":42},"line",1,[39,44,46],{"class":45},"svdQ7","const",[39,48,50],{"class":49},"snvgF"," values",[39,52,53],{"class":45}," =",[39,55,57],{"class":56},"ssxIu"," [",[39,59,61],{"class":60},"sJ6F3","\"\"",[39,63,64],{"class":56},", ",[39,66,67],{"class":49},"0",[39,69,64],{"class":56},[39,71,72],{"class":60},"\"0\"",[39,74,75],{"class":56},", [], {}, ",[39,77,78],{"class":49},"null",[39,80,64],{"class":56},[39,82,83],{"class":49},"undefined",[39,85,64],{"class":56},[39,87,88],{"class":49},"NaN",[39,90,91],{"class":56},"];\n",[93,94,97,121,140,162],"ul",{"className":95},[96],"contains-task-list",[98,99,102,107,108,64,110,64,112,64,114,64,116,64,118,120],"li",{"className":100},[101],"task-list-item",[103,104],"input",{"disabled":105,"type":106},true,"checkbox"," ",[23,109,61],{},[23,111,67],{},[23,113,72],{},[23,115,78],{},[23,117,83],{},[23,119,88],{}," — six of the eight",[98,122,124,107,126,64,128,64,130,64,132,64,134,136,137,139],{"className":123},[101],[103,125],{"disabled":105,"type":106},[23,127,61],{},[23,129,67],{},[23,131,78],{},[23,133,83],{},[23,135,88],{}," — five values (",[23,138,72],{}," is truthy since it's a non-empty string)",[98,141,143,107,145,64,147,64,149,64,152,64,155,64,157,64,159,161],{"className":142},[101],[103,144],{"disabled":105,"type":106},[23,146,61],{},[23,148,67],{},[23,150,151],{},"[]",[23,153,154],{},"{}",[23,156,78],{},[23,158,83],{},[23,160,88],{}," — seven values",[98,163,165,167],{"className":164},[101],[103,166],{"disabled":105,"type":106}," All eight values are falsy",[169,170,171,175,206],"details",{},[172,173,174],"summary",{},"Show Answer",[176,177,178,182,183,64,185,64,187,64,189,64,191,193,194,197,198,201,202,205],"p",{},[179,180,181],"strong",{},"Answer:"," B — ",[23,184,61],{},[23,186,67],{},[23,188,78],{},[23,190,83],{},[23,192,88],{}," (plus ",[23,195,196],{},"false"," and ",[23,199,200],{},"0n"," \u002F ",[23,203,204],{},"-0",", not listed here) are the only falsy values",[176,207,208,211,212,64,214,64,216,64,218,64,220,64,222,64,224,226,227,229,230,232,233,241,242,244,245,247],{},[179,209,210],{},"Explanation:"," JavaScript has a fixed, short list of falsy values: ",[23,213,196],{},[23,215,67],{},[23,217,204],{},[23,219,200],{},[23,221,61],{},[23,223,78],{},[23,225,83],{},", and ",[23,228,88],{},". Everything else is truthy — including ",[23,231,72],{}," (a non-empty string is always truthy regardless of its content) and, critically, ",[179,234,235,236,238,239],{},"both empty arrays ",[23,237,151],{}," and empty objects ",[23,240,154],{},", since any object reference is truthy no matter how \"empty\" it looks. Option C is the classic trap of assuming ",[23,243,151],{},"\u002F",[23,246,154],{}," are falsy because they \"feel empty\" — they are not.",[14,249,250,262,417,483],{"language":16},[18,251,253,254,257,258,261],{"id":252},"q2-what-happens-without-break-statements-in-a-switch","Q2. What happens without ",[23,255,256],{},"break"," statements in a ",[23,259,260],{},"switch","?",[28,263,264],{"language":16},[31,265,267],{"className":33,"code":266,"language":16,"meta":35,"style":35},"function getDiscount(tier) {\n  let discount;\n  switch (tier) {\n    case \"gold\":\n      discount = 20;\n    case \"silver\":\n      discount = 10;\n    default:\n      discount = 0;\n  }\n  return discount;\n}\nconsole.log(getDiscount(\"gold\"));\n",[23,268,269,288,297,306,318,333,343,355,363,375,381,389,395],{"__ignoreMap":35},[39,270,271,274,278,281,285],{"class":41,"line":42},[39,272,273],{"class":45},"function",[39,275,277],{"class":276},"sIsaT"," getDiscount",[39,279,280],{"class":56},"(",[39,282,284],{"class":283},"sCrzJ","tier",[39,286,287],{"class":56},") {\n",[39,289,291,294],{"class":41,"line":290},2,[39,292,293],{"class":45},"  let",[39,295,296],{"class":56}," discount;\n",[39,298,300,303],{"class":41,"line":299},3,[39,301,302],{"class":45},"  switch",[39,304,305],{"class":56}," (tier) {\n",[39,307,309,312,315],{"class":41,"line":308},4,[39,310,311],{"class":45},"    case",[39,313,314],{"class":60}," \"gold\"",[39,316,317],{"class":56},":\n",[39,319,321,324,327,330],{"class":41,"line":320},5,[39,322,323],{"class":56},"      discount ",[39,325,326],{"class":45},"=",[39,328,329],{"class":49}," 20",[39,331,332],{"class":56},";\n",[39,334,336,338,341],{"class":41,"line":335},6,[39,337,311],{"class":45},[39,339,340],{"class":60}," \"silver\"",[39,342,317],{"class":56},[39,344,346,348,350,353],{"class":41,"line":345},7,[39,347,323],{"class":56},[39,349,326],{"class":45},[39,351,352],{"class":49}," 10",[39,354,332],{"class":56},[39,356,358,361],{"class":41,"line":357},8,[39,359,360],{"class":45},"    default",[39,362,317],{"class":56},[39,364,366,368,370,373],{"class":41,"line":365},9,[39,367,323],{"class":56},[39,369,326],{"class":45},[39,371,372],{"class":49}," 0",[39,374,332],{"class":56},[39,376,378],{"class":41,"line":377},10,[39,379,380],{"class":56},"  }\n",[39,382,384,387],{"class":41,"line":383},11,[39,385,386],{"class":45},"  return",[39,388,296],{"class":56},[39,390,392],{"class":41,"line":391},12,[39,393,394],{"class":56},"}\n",[39,396,398,401,404,406,409,411,414],{"class":41,"line":397},13,[39,399,400],{"class":56},"console.",[39,402,403],{"class":276},"log",[39,405,280],{"class":56},[39,407,408],{"class":276},"getDiscount",[39,410,280],{"class":56},[39,412,413],{"class":60},"\"gold\"",[39,415,416],{"class":56},"));\n",[93,418,420,433,460,469],{"className":419},[96],[98,421,423,107,425,428,429,432],{"className":422},[101],[103,424],{"disabled":105,"type":106},[23,426,427],{},"20",", because ",[23,430,431],{},"case \"gold\""," matches and returns immediately",[98,434,436,107,438,440,441,443,444,447,448,450,451,454,455,457,458],{"className":435},[101],[103,437],{"disabled":105,"type":106},[23,439,67],{},", because without ",[23,442,256],{},", execution \"falls through\" every subsequent case (including ",[23,445,446],{},"default",") until a ",[23,449,256],{}," or the end of the block, so ",[23,452,453],{},"discount"," gets overwritten by each case in sequence, ending on ",[23,456,446],{},"'s ",[23,459,67],{},[98,461,463,107,465,468],{"className":462},[101],[103,464],{"disabled":105,"type":106},[23,466,467],{},"10",", because it falls through only one level before stopping",[98,470,472,107,474,428,477,479,480,482],{"className":471},[101],[103,473],{"disabled":105,"type":106},[23,475,476],{},"SyntaxError",[23,478,260],{}," cases require ",[23,481,256],{}," statements",[169,484,485,487,497],{},[172,486,174],{},[176,488,489,182,491,493,494,496],{},[179,490,181],{},[23,492,67],{},", due to fallthrough overwriting ",[23,495,453],{}," in every subsequent case",[176,498,499,107,501,504,505,64,507,509,510,513,514,516,517,519,520,522,523,525,526,529,530,457,533,529,536,457,538,541,542,544,545,547,548,551,552,555],{},[179,500,210],{},[179,502,503],{},"Debug:"," without an explicit ",[23,506,256],{},[23,508,260],{}," doesn't stop at the matched case — it keeps executing every statement below it, including other ",[23,511,512],{},"case"," bodies and ",[23,515,446],{},", until it hits a ",[23,518,256],{}," or the end of the ",[23,521,260],{}," block. Here, matching ",[23,524,413],{}," sets ",[23,527,528],{},"discount = 20",", then falls through to ",[23,531,532],{},"\"silver\"",[23,534,535],{},"discount = 10",[23,537,446],{},[23,539,540],{},"discount = 0",", leaving ",[23,543,67],{}," as the final value returned. This is one of the most common ",[23,546,260],{}," bugs — the fix is adding ",[23,549,550],{},"break;"," after each case (or ",[23,553,554],{},"return"," directly, if inside a function).",[14,557,558,568,644,721],{"language":16},[18,559,561,562,197,565,261],{"id":560},"q3-whats-the-key-difference-between-forin-and-forof","Q3. What's the key difference between ",[23,563,564],{},"for...in",[23,566,567],{},"for...of",[28,569,570],{"language":16},[31,571,573],{"className":33,"code":572,"language":16,"meta":35,"style":35},"const arr = [\"a\", \"b\", \"c\"];\nfor (const x in arr) console.log(x);\nfor (const x of arr) console.log(x);\n",[23,574,575,601,625],{"__ignoreMap":35},[39,576,577,579,582,584,586,589,591,594,596,599],{"class":41,"line":42},[39,578,46],{"class":45},[39,580,581],{"class":49}," arr",[39,583,53],{"class":45},[39,585,57],{"class":56},[39,587,588],{"class":60},"\"a\"",[39,590,64],{"class":56},[39,592,593],{"class":60},"\"b\"",[39,595,64],{"class":56},[39,597,598],{"class":60},"\"c\"",[39,600,91],{"class":56},[39,602,603,606,609,611,614,617,620,622],{"class":41,"line":290},[39,604,605],{"class":45},"for",[39,607,608],{"class":56}," (",[39,610,46],{"class":45},[39,612,613],{"class":49}," x",[39,615,616],{"class":45}," in",[39,618,619],{"class":56}," arr) console.",[39,621,403],{"class":276},[39,623,624],{"class":56},"(x);\n",[39,626,627,629,631,633,635,638,640,642],{"class":41,"line":299},[39,628,605],{"class":45},[39,630,608],{"class":56},[39,632,46],{"class":45},[39,634,613],{"class":49},[39,636,637],{"class":45}," of",[39,639,619],{"class":56},[39,641,403],{"class":276},[39,643,624],{"class":56},[93,645,647,659,695,706],{"className":646},[96],[98,648,650,652,653,64,655,64,657],{"className":649},[101],[103,651],{"disabled":105,"type":106}," They are interchangeable for arrays; both log ",[23,654,588],{},[23,656,593],{},[23,658,598],{},[98,660,662,107,664,666,667,671,672,64,674,64,677,680,681,683,684,687,688,64,690,64,692,694],{"className":661},[101],[103,663],{"disabled":105,"type":106},[23,665,564],{}," iterates over enumerable property ",[668,669,670],"em",{},"keys"," (here, the string indices ",[23,673,72],{},[23,675,676],{},"\"1\"",[23,678,679],{},"\"2\"","); ",[23,682,567],{}," iterates over the ",[668,685,686],{},"values"," of an iterable (here, ",[23,689,588],{},[23,691,593],{},[23,693,598],{},")",[98,696,698,107,700,702,703,705],{"className":697},[101],[103,699],{"disabled":105,"type":106},[23,701,564],{}," iterates values, ",[23,704,567],{}," iterates keys — the reverse of the truth",[98,707,709,107,711,713,714,244,717,720],{"className":708},[101],[103,710],{"disabled":105,"type":106},[23,712,567],{}," only works on ",[23,715,716],{},"Map",[23,718,719],{},"Set",", never on plain arrays",[169,722,723,725,735],{},[172,724,174],{},[176,726,727,182,729,731,732,734],{},[179,728,181],{},[23,730,564],{}," yields keys (indices as strings for arrays); ",[23,733,567],{}," yields values",[176,736,737,107,739,741,742,744,745,64,747,64,749,751,752,754,755,64,757,759,760,107,763,765,766,768],{},[179,738,210],{},[23,740,564],{}," enumerates property ",[668,743,670],{}," of any object, including inherited enumerable ones — for an array, that means it logs the string indices ",[23,746,72],{},[23,748,676],{},[23,750,679],{}," (not the values, and not necessarily in guaranteed numeric order for non-index keys). ",[23,753,567],{}," (ES2015) works on anything implementing the iterable protocol (arrays, strings, ",[23,756,716],{},[23,758,719],{},", etc.) and yields the actual values in order. ",[179,761,762],{},"Idiom:",[23,764,564],{}," is generally discouraged for arrays specifically because of this keys-not-values behavior plus the risk of picking up inherited\u002Fnon-index enumerable properties; ",[23,767,567],{}," (or array methods) is preferred.",[14,770,771,783,902,965],{"language":16},[18,772,774,775,778,779,782],{"id":773},"q4-what-is-the-classic-closure-in-loop-bug-with-var-and-how-does-let-fix-it","Q4. What is the classic closure-in-loop bug with ",[23,776,777],{},"var",", and how does ",[23,780,781],{},"let"," fix it?",[28,784,785],{"language":16},[31,786,788],{"className":33,"code":787,"language":16,"meta":35,"style":35},"for (var i = 0; i \u003C 3; i++) {\n  setTimeout(() => console.log(i), 0);\n}\nfor (let j = 0; j \u003C 3; j++) {\n  setTimeout(() => console.log(j), 0);\n}\n",[23,789,790,822,846,850,879,898],{"__ignoreMap":35},[39,791,792,794,796,798,801,803,805,808,811,814,817,820],{"class":41,"line":42},[39,793,605],{"class":45},[39,795,608],{"class":56},[39,797,777],{"class":45},[39,799,800],{"class":56}," i ",[39,802,326],{"class":45},[39,804,372],{"class":49},[39,806,807],{"class":56},"; i ",[39,809,810],{"class":45},"\u003C",[39,812,813],{"class":49}," 3",[39,815,816],{"class":56},"; i",[39,818,819],{"class":45},"++",[39,821,287],{"class":56},[39,823,824,827,830,833,836,838,841,843],{"class":41,"line":290},[39,825,826],{"class":276},"  setTimeout",[39,828,829],{"class":56},"(() ",[39,831,832],{"class":45},"=>",[39,834,835],{"class":56}," console.",[39,837,403],{"class":276},[39,839,840],{"class":56},"(i), ",[39,842,67],{"class":49},[39,844,845],{"class":56},");\n",[39,847,848],{"class":41,"line":299},[39,849,394],{"class":56},[39,851,852,854,856,858,861,863,865,868,870,872,875,877],{"class":41,"line":308},[39,853,605],{"class":45},[39,855,608],{"class":56},[39,857,781],{"class":45},[39,859,860],{"class":56}," j ",[39,862,326],{"class":45},[39,864,372],{"class":49},[39,866,867],{"class":56},"; j ",[39,869,810],{"class":45},[39,871,813],{"class":49},[39,873,874],{"class":56},"; j",[39,876,819],{"class":45},[39,878,287],{"class":56},[39,880,881,883,885,887,889,891,894,896],{"class":41,"line":320},[39,882,826],{"class":276},[39,884,829],{"class":56},[39,886,832],{"class":45},[39,888,835],{"class":56},[39,890,403],{"class":276},[39,892,893],{"class":56},"(j), ",[39,895,67],{"class":49},[39,897,845],{"class":56},[39,899,900],{"class":41,"line":335},[39,901,394],{"class":56},[93,903,905,914,943,958],{"className":904},[96],[98,906,908,910,911],{"className":907},[101],[103,909],{"disabled":105,"type":106}," Both loops log ",[23,912,913],{},"0, 1, 2",[98,915,917,919,920,922,923,926,927,930,931,934,935,922,937,939,940,694],{"className":916},[101],[103,918],{"disabled":105,"type":106}," The ",[23,921,777],{}," loop logs ",[23,924,925],{},"3, 3, 3"," (all closures share the single function-scoped ",[23,928,929],{},"i",", which is ",[23,932,933],{},"3"," by the time the callbacks run); the ",[23,936,781],{},[23,938,913],{}," (each iteration gets its own freshly bound ",[23,941,942],{},"j",[98,944,946,919,948,922,950,952,953,922,955,957],{"className":945},[101],[103,947],{"disabled":105,"type":106},[23,949,777],{},[23,951,913],{}," and the ",[23,954,781],{},[23,956,925],{}," — the reverse",[98,959,961,910,963],{"className":960},[101],[103,962],{"disabled":105,"type":106},[23,964,925],{},[169,966,967,969,982],{},[172,968,174],{},[176,970,971,182,973,922,975,977,978,922,980],{},[179,972,181],{},[23,974,777],{},[23,976,925],{},"; ",[23,979,781],{},[23,981,913],{},[176,983,984,107,986,988,989,992,993,107,996,998,999,1002,1003,1005,1006,1008,1009,1011,1012,1015,1016,1022,1023,1025,1026,1028,1029,1031],{},[179,985,210],{},[179,987,503],{}," this is one of the most famous JavaScript interview gotchas. ",[23,990,991],{},"var i"," is function\u002Fglobal-scoped, so there is only ever ",[668,994,995],{},"one",[23,997,929],{}," variable shared across all loop iterations and all the closures created inside it; by the time the ",[23,1000,1001],{},"setTimeout"," callbacks actually run (after the loop has fully finished), ",[23,1004,929],{}," has already reached ",[23,1007,933],{},". ",[23,1010,781],{}," fixes this because the spec gives ",[23,1013,1014],{},"for (let j ...)"," a ",[179,1017,1018,1019,1021],{},"fresh binding of ",[23,1020,942],{}," for each iteration"," — each closure captures its own distinct ",[23,1024,942],{},", preserving the value at the time that iteration ran. This is a core reason ",[23,1027,781],{}," is preferred over ",[23,1030,777],{}," in loops with async callbacks.",[14,1033,1034,1042,1150,1198],{"language":16},[18,1035,1037,1038,1041],{"id":1036},"q5-what-does-a-labeled-continue-statement-do-inside-nested-loops","Q5. What does a labeled ",[23,1039,1040],{},"continue"," statement do inside nested loops?",[28,1043,1044],{"language":16},[31,1045,1047],{"className":33,"code":1046,"language":16,"meta":35,"style":35},"outer: for (let i = 0; i \u003C 3; i++) {\n  for (let j = 0; j \u003C 3; j++) {\n    if (j === 1) continue outer;\n    console.log(i, j);\n  }\n}\n",[23,1048,1049,1081,1108,1132,1142,1146],{"__ignoreMap":35},[39,1050,1051,1054,1057,1059,1061,1063,1065,1067,1069,1071,1073,1075,1077,1079],{"class":41,"line":42},[39,1052,1053],{"class":276},"outer",[39,1055,1056],{"class":56},": ",[39,1058,605],{"class":45},[39,1060,608],{"class":56},[39,1062,781],{"class":45},[39,1064,800],{"class":56},[39,1066,326],{"class":45},[39,1068,372],{"class":49},[39,1070,807],{"class":56},[39,1072,810],{"class":45},[39,1074,813],{"class":49},[39,1076,816],{"class":56},[39,1078,819],{"class":45},[39,1080,287],{"class":56},[39,1082,1083,1086,1088,1090,1092,1094,1096,1098,1100,1102,1104,1106],{"class":41,"line":290},[39,1084,1085],{"class":45},"  for",[39,1087,608],{"class":56},[39,1089,781],{"class":45},[39,1091,860],{"class":56},[39,1093,326],{"class":45},[39,1095,372],{"class":49},[39,1097,867],{"class":56},[39,1099,810],{"class":45},[39,1101,813],{"class":49},[39,1103,874],{"class":56},[39,1105,819],{"class":45},[39,1107,287],{"class":56},[39,1109,1110,1113,1116,1119,1122,1125,1127,1130],{"class":41,"line":299},[39,1111,1112],{"class":45},"    if",[39,1114,1115],{"class":56}," (j ",[39,1117,1118],{"class":45},"===",[39,1120,1121],{"class":49}," 1",[39,1123,1124],{"class":56},") ",[39,1126,1040],{"class":45},[39,1128,1129],{"class":276}," outer",[39,1131,332],{"class":56},[39,1133,1134,1137,1139],{"class":41,"line":308},[39,1135,1136],{"class":56},"    console.",[39,1138,403],{"class":276},[39,1140,1141],{"class":56},"(i, j);\n",[39,1143,1144],{"class":41,"line":320},[39,1145,380],{"class":56},[39,1147,1148],{"class":41,"line":335},[39,1149,394],{"class":56},[93,1151,1153,1162,1177,1185],{"className":1152},[96],[98,1154,1156,1158,1159,1161],{"className":1155},[101],[103,1157],{"disabled":105,"type":106}," It behaves like a normal ",[23,1160,1040],{},", only skipping the rest of the inner loop's current iteration",[98,1163,1165,1167,1168,64,1171,64,1174],{"className":1164},[101],[103,1166],{"disabled":105,"type":106}," It skips the rest of the inner loop entirely and jumps straight to the next iteration of the labeled outer loop, effectively logging only ",[23,1169,1170],{},"(0,0)",[23,1172,1173],{},"(1,0)",[23,1175,1176],{},"(2,0)",[98,1178,1180,1182,1183],{"className":1179},[101],[103,1181],{"disabled":105,"type":106}," It exits both loops entirely, like a labeled ",[23,1184,256],{},[98,1186,1188,107,1190,1192,1193,1195,1196],{"className":1187},[101],[103,1189],{"disabled":105,"type":106},[23,1191,476],{}," — labels can only be used with ",[23,1194,256],{},", not ",[23,1197,1040],{},[169,1199,1200,1202,1213],{},[172,1201,174],{},[176,1203,1204,1206,1207,64,1209,64,1211],{},[179,1205,181],{}," B — Jumps to the next iteration of the labeled outer loop, logging only ",[23,1208,1170],{},[23,1210,1173],{},[23,1212,1176],{},[176,1214,1215,1217,1218,1220,1221,64,1224,1227,1228,1230,1231,1233,1234,1236,1237,1240,1241,1244,1245,1247,1248,1250,1251,1253,1254,1256,1257,197,1259,1261],{},[179,1216,210],{}," A labeled ",[23,1219,1040],{}," targets a specific enclosing loop by name, skipping straight to that loop's next iteration rather than just the innermost one. Here, once ",[23,1222,1223],{},"j === 1",[23,1225,1226],{},"continue outer"," abandons the rest of the inner loop for that ",[23,1229,929],{}," and advances ",[23,1232,929],{}," instead — so ",[23,1235,942],{}," never reaches ",[23,1238,1239],{},"1"," or ",[23,1242,1243],{},"2"," in the output. Option A describes plain unlabeled ",[23,1246,1040],{},". Option C describes labeled ",[23,1249,256],{},", a related but distinct construct (",[23,1252,256],{}," exits the loop entirely; ",[23,1255,1040],{}," advances it). Labels work with both ",[23,1258,256],{},[23,1260,1040],{}," (ruling out D), though labels are rare in idiomatic code outside deeply nested loop scenarios like this.",[14,1263,1264,1272,1383,1433],{"language":16},[18,1265,1267,1268,1271],{"id":1266},"q6-what-is-the-output-of-this-ifelse-ifelse-chain-given-javascripts-truthyfalsy-coercion","Q6. What is the output of this ",[23,1269,1270],{},"if\u002Felse if\u002Felse"," chain, given JavaScript's truthy\u002Ffalsy coercion?",[28,1273,1274],{"language":16},[31,1275,1277],{"className":33,"code":1276,"language":16,"meta":35,"style":35},"function describe(val) {\n  if (val) {\n    return \"truthy\";\n  } else if (val === 0) {\n    return \"zero\";\n  } else {\n    return \"other falsy\";\n  }\n}\nconsole.log(describe(0));\n",[23,1278,1279,1293,1301,1311,1331,1340,1349,1358,1362,1366],{"__ignoreMap":35},[39,1280,1281,1283,1286,1288,1291],{"class":41,"line":42},[39,1282,273],{"class":45},[39,1284,1285],{"class":276}," describe",[39,1287,280],{"class":56},[39,1289,1290],{"class":283},"val",[39,1292,287],{"class":56},[39,1294,1295,1298],{"class":41,"line":290},[39,1296,1297],{"class":45},"  if",[39,1299,1300],{"class":56}," (val) {\n",[39,1302,1303,1306,1309],{"class":41,"line":299},[39,1304,1305],{"class":45},"    return",[39,1307,1308],{"class":60}," \"truthy\"",[39,1310,332],{"class":56},[39,1312,1313,1316,1319,1322,1325,1327,1329],{"class":41,"line":308},[39,1314,1315],{"class":56},"  } ",[39,1317,1318],{"class":45},"else",[39,1320,1321],{"class":45}," if",[39,1323,1324],{"class":56}," (val ",[39,1326,1118],{"class":45},[39,1328,372],{"class":49},[39,1330,287],{"class":56},[39,1332,1333,1335,1338],{"class":41,"line":320},[39,1334,1305],{"class":45},[39,1336,1337],{"class":60}," \"zero\"",[39,1339,332],{"class":56},[39,1341,1342,1344,1346],{"class":41,"line":335},[39,1343,1315],{"class":56},[39,1345,1318],{"class":45},[39,1347,1348],{"class":56}," {\n",[39,1350,1351,1353,1356],{"class":41,"line":345},[39,1352,1305],{"class":45},[39,1354,1355],{"class":60}," \"other falsy\"",[39,1357,332],{"class":56},[39,1359,1360],{"class":41,"line":357},[39,1361,380],{"class":56},[39,1363,1364],{"class":41,"line":365},[39,1365,394],{"class":56},[39,1367,1368,1370,1372,1374,1377,1379,1381],{"class":41,"line":377},[39,1369,400],{"class":56},[39,1371,403],{"class":276},[39,1373,280],{"class":56},[39,1375,1376],{"class":276},"describe",[39,1378,280],{"class":56},[39,1380,67],{"class":49},[39,1382,416],{"class":56},[93,1384,1386,1398,1409,1425],{"className":1385},[96],[98,1387,1389,107,1391,1394,1395,1397],{"className":1388},[101],[103,1390],{"disabled":105,"type":106},[23,1392,1393],{},"\"truthy\"",", since ",[23,1396,67],{}," matches the first branch",[98,1399,1401,107,1403,1406,1407],{"className":1400},[101],[103,1402],{"disabled":105,"type":106},[23,1404,1405],{},"\"other falsy\"",", since it skips straight to the ",[23,1408,1318],{},[98,1410,1412,107,1414,1417,1418,1420,1421,1424],{"className":1411},[101],[103,1413],{"disabled":105,"type":106},[23,1415,1416],{},"\"zero\""," never runs, because ",[23,1419,67],{}," is falsy and the ",[23,1422,1423],{},"if (val)"," branch has already consumed it — this is actually unreachable dead code",[98,1426,1428,107,1430],{"className":1427},[101],[103,1429],{"disabled":105,"type":106},[23,1431,1432],{},"TypeError",[169,1434,1435,1437,1447],{},[172,1436,174],{},[176,1438,1439,1441,1442,1444,1445],{},[179,1440,181],{}," C — ",[23,1443,1416],{}," is dead code; the function actually returns ",[23,1446,1405],{},[176,1448,1449,107,1451,1453,1454,1456,1457,1460,1461,1463,1464,1466,1467,977,1469,1472,1473,1466,1476,1479,1480,1483,1484,1486,1487,1489,1490,1493,1494,1497,1498,1500,1501,1504,1505,1507],{},[179,1450,210],{},[23,1452,1423],{}," checks truthiness first, and ",[23,1455,67],{}," is falsy, so the first branch is skipped — flow moves to ",[23,1458,1459],{},"else if (val === 0)",", which correctly matches, returning ",[23,1462,1416],{},". Wait — trace it carefully: ",[23,1465,1290],{}," is ",[23,1468,67],{},[23,1470,1471],{},"if (0)"," is falsy, skip; ",[23,1474,1475],{},"else if (0 === 0)",[23,1477,1478],{},"true",", so it ",[668,1481,1482],{},"does"," return ",[23,1485,1416],{},". Correcting the above: the actual output is ",[23,1488,1416],{},", not dead code — the ",[23,1491,1492],{},"else if"," condition is reachable precisely ",[668,1495,1496],{},"because"," the first branch failed. The important lesson is that a truthiness check (",[23,1499,1423],{},") and a strict equality check (",[23,1502,1503],{},"val === 0",") test different things, and ordering matters: had the branches been swapped, results would differ. Option A wrongly assumes truthy already caught ",[23,1506,67],{},". Option D invents an error that doesn't occur.",[14,1509,1510,1517,1597,1647],{"language":16},[18,1511,1513,1514,1516],{"id":1512},"q7-what-does-a-for-loop-do-when-the-condition-is-omitted-entirely","Q7. What does a ",[23,1515,605],{}," loop do when the condition is omitted entirely?",[28,1518,1519],{"language":16},[31,1520,1522],{"className":33,"code":1521,"language":16,"meta":35,"style":35},"let count = 0;\nfor (let i = 0; ; i++) {\n  count++;\n  if (count === 3) break;\n}\nconsole.log(count);\n",[23,1523,1524,1537,1558,1567,1584,1588],{"__ignoreMap":35},[39,1525,1526,1528,1531,1533,1535],{"class":41,"line":42},[39,1527,781],{"class":45},[39,1529,1530],{"class":56}," count ",[39,1532,326],{"class":45},[39,1534,372],{"class":49},[39,1536,332],{"class":56},[39,1538,1539,1541,1543,1545,1547,1549,1551,1554,1556],{"class":41,"line":290},[39,1540,605],{"class":45},[39,1542,608],{"class":56},[39,1544,781],{"class":45},[39,1546,800],{"class":56},[39,1548,326],{"class":45},[39,1550,372],{"class":49},[39,1552,1553],{"class":56},"; ; i",[39,1555,819],{"class":45},[39,1557,287],{"class":56},[39,1559,1560,1563,1565],{"class":41,"line":299},[39,1561,1562],{"class":56},"  count",[39,1564,819],{"class":45},[39,1566,332],{"class":56},[39,1568,1569,1571,1574,1576,1578,1580,1582],{"class":41,"line":308},[39,1570,1297],{"class":45},[39,1572,1573],{"class":56}," (count ",[39,1575,1118],{"class":45},[39,1577,813],{"class":49},[39,1579,1124],{"class":56},[39,1581,256],{"class":45},[39,1583,332],{"class":56},[39,1585,1586],{"class":41,"line":320},[39,1587,394],{"class":56},[39,1589,1590,1592,1594],{"class":41,"line":335},[39,1591,400],{"class":56},[39,1593,403],{"class":276},[39,1595,1596],{"class":56},"(count);\n",[93,1598,1600,1611,1619,1637],{"className":1599},[96],[98,1601,1603,107,1605,1607,1608,1610],{"className":1602},[101],[103,1604],{"disabled":105,"type":106},[23,1606,476],{},", because the middle clause of a ",[23,1609,605],{}," loop is mandatory",[98,1612,1614,1616,1617],{"className":1613},[101],[103,1615],{"disabled":105,"type":106}," The loop runs zero times, since an omitted condition defaults to ",[23,1618,196],{},[98,1620,1622,1624,1625,1627,1628,1631,1632,1634,1635],{"className":1621},[101],[103,1623],{"disabled":105,"type":106}," The loop runs forever, since an omitted condition defaults to always-truthy — but here ",[23,1626,256],{}," stops it manually once ",[23,1629,1630],{},"count"," reaches ",[23,1633,933],{},", so it logs ",[23,1636,933],{},[98,1638,1640,107,1642,1394,1644,1646],{"className":1639},[101],[103,1641],{"disabled":105,"type":106},[23,1643,83],{},[23,1645,1630],{}," is never properly initialized",[169,1648,1649,1651,1659],{},[172,1650,174],{},[176,1652,1653,1655,1656,1658],{},[179,1654,181],{}," C — Omitted condition defaults to always-true (infinite loop) unless stopped manually; logs ",[23,1657,933],{}," here",[176,1660,1661,1663,1664,1667,1668,1671,1672,1674,1675,1631,1677,1679,1680,1682],{},[179,1662,210],{}," All three clauses of a classic ",[23,1665,1666],{},"for (init; condition; update)"," loop are optional. An omitted condition is treated as always truthy, making ",[23,1669,1670],{},"for (let i = 0; ; i++)"," loop forever by default — this is a legitimate, if unusual, idiom for an intentionally infinite loop that relies entirely on an internal ",[23,1673,256],{}," to terminate (as shown here, breaking once ",[23,1676,1630],{},[23,1678,933],{},"). Option A is wrong; omitting any\u002Fall of the three ",[23,1681,605],{}," clauses is valid syntax. Option B inverts the actual default.",[14,1684,1685,1697,1747,1796],{"language":16},[18,1686,1688,1689,1691,1692,1694,1695,26],{"id":1687},"q8-what-happens-when-a-forof-loop-is-used-on-a-plain-object-not-an-array-map-or-set","Q8. What happens when a ",[23,1690,567],{}," loop is used on a plain object (not an array, ",[23,1693,716],{},", or ",[23,1696,719],{},[28,1698,1699],{"language":16},[31,1700,1702],{"className":33,"code":1701,"language":16,"meta":35,"style":35},"const obj = { a: 1, b: 2 };\nfor (const val of obj) console.log(val);\n",[23,1703,1704,1726],{"__ignoreMap":35},[39,1705,1706,1708,1711,1713,1716,1718,1721,1723],{"class":41,"line":42},[39,1707,46],{"class":45},[39,1709,1710],{"class":49}," obj",[39,1712,53],{"class":45},[39,1714,1715],{"class":56}," { a: ",[39,1717,1239],{"class":49},[39,1719,1720],{"class":56},", b: ",[39,1722,1243],{"class":49},[39,1724,1725],{"class":56}," };\n",[39,1727,1728,1730,1732,1734,1737,1739,1742,1744],{"class":41,"line":290},[39,1729,605],{"class":45},[39,1731,608],{"class":56},[39,1733,46],{"class":45},[39,1735,1736],{"class":49}," val",[39,1738,637],{"class":45},[39,1740,1741],{"class":56}," obj) console.",[39,1743,403],{"class":276},[39,1745,1746],{"class":56},"(val);\n",[93,1748,1750,1762,1772,1786],{"className":1749},[96],[98,1751,1753,1755,1756,1758,1759,1761],{"className":1752},[101],[103,1754],{"disabled":105,"type":106}," Logs ",[23,1757,1239],{}," then ",[23,1760,1243],{},", the object's values",[98,1763,1765,1755,1767,1758,1769,1771],{"className":1764},[101],[103,1766],{"disabled":105,"type":106},[23,1768,588],{},[23,1770,593],{},", the object's keys",[98,1773,1775,1777,1778,1781,1782,1785],{"className":1774},[101],[103,1776],{"disabled":105,"type":106}," Throws ",[23,1779,1780],{},"TypeError: obj is not iterable",", because plain objects don't implement the iterable protocol (",[23,1783,1784],{},"Symbol.iterator",") by default",[98,1787,1789,1791,1792,1795],{"className":1788},[101],[103,1790],{"disabled":105,"type":106}," Logs nothing, silently, since the object has no ",[23,1793,1794],{},"length"," property",[169,1797,1798,1800,1807],{},[172,1799,174],{},[176,1801,1802,1804,1805],{},[179,1803,181],{}," C — Throws ",[23,1806,1780],{},[176,1808,1809,107,1811,107,1813,1815,1816,1819,1820,64,1822,1824,1825,1827,1828,1830,1831,244,1834,244,1837,1840,1841,1843,1844,1846,1847,1849],{},[179,1810,210],{},[179,1812,503],{},[23,1814,567],{}," requires its target to implement the iterable protocol (i.e., have a ",[23,1817,1818],{},"[Symbol.iterator]"," method) — arrays, strings, ",[23,1821,716],{},[23,1823,719],{},", and generators do; plain objects (",[23,1826,154],{},") do not, by design, since object key order\u002Fstructure isn't inherently sequential the way a list is. The correct way to iterate a plain object's entries is ",[23,1829,564],{}," (keys only), or ",[23,1832,1833],{},"Object.entries(obj)",[23,1835,1836],{},"Object.keys(obj)",[23,1838,1839],{},"Object.values(obj)"," combined with ",[23,1842,567],{}," on the resulting array. This is a common runtime error when developers assume ",[23,1845,567],{}," works universally like ",[23,1848,564],{}," does.",[14,1851,1852,1859,1961,2022],{"language":16},[18,1853,1855,1856,1858],{"id":1854},"q9-what-is-the-output-when-a-switch-uses-strict-comparison-against-mixed-types","Q9. What is the output when a ",[23,1857,260],{}," uses strict comparison against mixed types?",[28,1860,1861],{"language":16},[31,1862,1864],{"className":33,"code":1863,"language":16,"meta":35,"style":35},"function check(val) {\n  switch (val) {\n    case \"1\":\n      return \"string one\";\n    case 1:\n      return \"number one\";\n    default:\n      return \"no match\";\n  }\n}\nconsole.log(check(1));\n",[23,1865,1866,1879,1885,1894,1904,1912,1921,1927,1936,1940,1944],{"__ignoreMap":35},[39,1867,1868,1870,1873,1875,1877],{"class":41,"line":42},[39,1869,273],{"class":45},[39,1871,1872],{"class":276}," check",[39,1874,280],{"class":56},[39,1876,1290],{"class":283},[39,1878,287],{"class":56},[39,1880,1881,1883],{"class":41,"line":290},[39,1882,302],{"class":45},[39,1884,1300],{"class":56},[39,1886,1887,1889,1892],{"class":41,"line":299},[39,1888,311],{"class":45},[39,1890,1891],{"class":60}," \"1\"",[39,1893,317],{"class":56},[39,1895,1896,1899,1902],{"class":41,"line":308},[39,1897,1898],{"class":45},"      return",[39,1900,1901],{"class":60}," \"string one\"",[39,1903,332],{"class":56},[39,1905,1906,1908,1910],{"class":41,"line":320},[39,1907,311],{"class":45},[39,1909,1121],{"class":49},[39,1911,317],{"class":56},[39,1913,1914,1916,1919],{"class":41,"line":335},[39,1915,1898],{"class":45},[39,1917,1918],{"class":60}," \"number one\"",[39,1920,332],{"class":56},[39,1922,1923,1925],{"class":41,"line":345},[39,1924,360],{"class":45},[39,1926,317],{"class":56},[39,1928,1929,1931,1934],{"class":41,"line":357},[39,1930,1898],{"class":45},[39,1932,1933],{"class":60}," \"no match\"",[39,1935,332],{"class":56},[39,1937,1938],{"class":41,"line":365},[39,1939,380],{"class":56},[39,1941,1942],{"class":41,"line":377},[39,1943,394],{"class":56},[39,1945,1946,1948,1950,1952,1955,1957,1959],{"class":41,"line":383},[39,1947,400],{"class":56},[39,1949,403],{"class":276},[39,1951,280],{"class":56},[39,1953,1954],{"class":276},"check",[39,1956,280],{"class":56},[39,1958,1239],{"class":49},[39,1960,416],{"class":56},[93,1962,1964,1984,2007,2016],{"className":1963},[96],[98,1965,1967,107,1969,428,1972,1974,1975,1978,1979,1981,1982],{"className":1966},[101],[103,1968],{"disabled":105,"type":106},[23,1970,1971],{},"\"string one\"",[23,1973,260],{}," uses ",[23,1976,1977],{},"=="," and coerces ",[23,1980,1239],{}," to ",[23,1983,676],{},[98,1985,1987,107,1989,428,1992,1994,1995,1997,1998,2000,2001,1195,2004],{"className":1986},[101],[103,1988],{"disabled":105,"type":106},[23,1990,1991],{},"\"number one\"",[23,1993,260],{}," compares using strict equality (",[23,1996,1118],{},"), so ",[23,1999,1239],{}," (a number) only matches ",[23,2002,2003],{},"case 1",[23,2005,2006],{},"case \"1\"",[98,2008,2010,107,2012,2015],{"className":2009},[101],[103,2011],{"disabled":105,"type":106},[23,2013,2014],{},"\"no match\"",", because neither case matches",[98,2017,2019,2021],{"className":2018},[101],[103,2020],{"disabled":105,"type":106}," Both cases match simultaneously and it returns the first one syntactically, regardless of type",[169,2023,2024,2026,2035],{},[172,2025,174],{},[176,2027,2028,182,2030,1394,2032,2034],{},[179,2029,181],{},[23,2031,1991],{},[23,2033,260],{}," compares with strict equality",[176,2036,2037,107,2039,2041,2042,2044,2045,2047,2048,2051,2052,2054,2055,2057,2058,2060,2061,2063,2064,2066,2067,2069,2070,2073,2074,2076],{},[179,2038,210],{},[179,2040,503],{}," a common misconception is that ",[23,2043,260],{}," behaves like loose ",[23,2046,1977],{}," comparison (similar to ",[23,2049,2050],{},"if (val == \"1\")","); it actually uses strict ",[23,2053,1118],{}," comparison internally, so type mismatches between the switched value and a ",[23,2056,512],{}," label never match, even if they'd be loosely equal. Since ",[23,2059,1290],{}," is the number ",[23,2062,1239],{},", it only matches ",[23,2065,2003],{},", skipping ",[23,2068,2006],{}," entirely. This matters in real code when switching on values that might arrive as strings (e.g., from ",[23,2071,2072],{},"URLSearchParams"," or form inputs) — an easy silent-fallthrough-to-",[23,2075,446],{}," bug if the type isn't what's expected.",[14,2078,2079,2086,2196,2241],{"language":16},[18,2080,2082,2083,2085],{"id":2081},"q10-what-does-an-empty-for-loop-body-with-a-semicolon-do","Q10. What does an empty ",[23,2084,605],{}," loop body with a semicolon do?",[28,2087,2088],{"language":16},[31,2089,2091],{"className":33,"code":2090,"language":16,"meta":35,"style":35},"const arr = [5, 3, 8, 1];\nlet max = arr[0];\nfor (let i = 1; i \u003C arr.length; max = arr[i] > max ? arr[i] : max, i++);\nconsole.log(max);\n",[23,2092,2093,2121,2137,2187],{"__ignoreMap":35},[39,2094,2095,2097,2099,2101,2103,2106,2108,2110,2112,2115,2117,2119],{"class":41,"line":42},[39,2096,46],{"class":45},[39,2098,581],{"class":49},[39,2100,53],{"class":45},[39,2102,57],{"class":56},[39,2104,2105],{"class":49},"5",[39,2107,64],{"class":56},[39,2109,933],{"class":49},[39,2111,64],{"class":56},[39,2113,2114],{"class":49},"8",[39,2116,64],{"class":56},[39,2118,1239],{"class":49},[39,2120,91],{"class":56},[39,2122,2123,2125,2128,2130,2133,2135],{"class":41,"line":290},[39,2124,781],{"class":45},[39,2126,2127],{"class":56}," max ",[39,2129,326],{"class":45},[39,2131,2132],{"class":56}," arr[",[39,2134,67],{"class":49},[39,2136,91],{"class":56},[39,2138,2139,2141,2143,2145,2147,2149,2151,2153,2155,2158,2160,2163,2165,2168,2171,2173,2175,2177,2180,2183,2185],{"class":41,"line":299},[39,2140,605],{"class":45},[39,2142,608],{"class":56},[39,2144,781],{"class":45},[39,2146,800],{"class":56},[39,2148,326],{"class":45},[39,2150,1121],{"class":49},[39,2152,807],{"class":56},[39,2154,810],{"class":45},[39,2156,2157],{"class":56}," arr.",[39,2159,1794],{"class":49},[39,2161,2162],{"class":56},"; max ",[39,2164,326],{"class":45},[39,2166,2167],{"class":56}," arr[i] ",[39,2169,2170],{"class":45},">",[39,2172,2127],{"class":56},[39,2174,261],{"class":45},[39,2176,2167],{"class":56},[39,2178,2179],{"class":45},":",[39,2181,2182],{"class":56}," max, i",[39,2184,819],{"class":45},[39,2186,845],{"class":56},[39,2188,2189,2191,2193],{"class":41,"line":308},[39,2190,400],{"class":56},[39,2192,403],{"class":276},[39,2194,2195],{"class":56},"(max);\n",[93,2197,2199,2210,2222,2230],{"className":2198},[96],[98,2200,2202,107,2204,2206,2207,2209],{"className":2201},[101],[103,2203],{"disabled":105,"type":106},[23,2205,476],{},", since a ",[23,2208,605],{}," loop body cannot be empty",[98,2211,2213,107,2215,2217,2218,2221],{"className":2212},[101],[103,2214],{"disabled":105,"type":106},[23,2216,2114],{}," — the loop still runs correctly; all the work happens in the update clause via the comma operator, and the trailing ",[23,2219,2220],{},";"," is simply an empty statement acting as the loop body",[98,2223,2225,107,2227,2229],{"className":2224},[101],[103,2226],{"disabled":105,"type":106},[23,2228,1239],{},", because the loop never actually executes the comparison",[98,2231,2233,107,2235,1394,2237,2240],{"className":2232},[101],[103,2234],{"disabled":105,"type":106},[23,2236,83],{},[23,2238,2239],{},"max"," is never reassigned",[169,2242,2243,2245,2252],{},[172,2244,174],{},[176,2246,2247,182,2249,2251],{},[179,2248,181],{},[23,2250,2114],{},"; the loop is legal, doing all its work in the update clause via the comma operator",[176,2253,2254,2256,2257,2259,2260,2263,2264,2267,2268,2270,2271,2273,2274,2276],{},[179,2255,210],{}," A lone ",[23,2258,2220],{}," immediately after a ",[23,2261,2262],{},"for(...)"," header is a valid (if unusual and generally discouraged) ",[668,2265,2266],{},"empty statement",", serving as the loop's entire body. This example is legal but poor style: it stuffs both the max-tracking logic and the increment into the update clause using the comma operator (see Operators topic Q10) so no loop body is needed at all. It does correctly compute the max (",[23,2269,2114],{},"). ",[179,2272,762],{}," despite being valid, this kind of \"do everything in the ",[23,2275,2262],{}," header\" pattern is considered bad practice — real code should put logic in the loop body for readability, reserving the empty-body pattern for rare, deliberate cases.",[14,2278,2279,2289,2384,2432],{"language":16},[18,2280,2282,2283,2285,2286,2288],{"id":2281},"q11-what-is-the-scoping-behavior-of-a-variable-declared-with-let-inside-a-switch-block-with-no-per-case-braces","Q11. What is the scoping behavior of a variable declared with ",[23,2284,781],{}," inside a ",[23,2287,260],{}," block with no per-case braces?",[28,2290,2291],{"language":16},[31,2292,2294],{"className":33,"code":2293,"language":16,"meta":35,"style":35},"function process(type) {\n  switch (type) {\n    case \"a\":\n      let result = \"A result\";\n      return result;\n    case \"b\":\n      let result = \"B result\";\n      return result;\n  }\n}\n",[23,2295,2296,2310,2317,2326,2341,2348,2357,2370,2376,2380],{"__ignoreMap":35},[39,2297,2298,2300,2303,2305,2308],{"class":41,"line":42},[39,2299,273],{"class":45},[39,2301,2302],{"class":276}," process",[39,2304,280],{"class":56},[39,2306,2307],{"class":283},"type",[39,2309,287],{"class":56},[39,2311,2312,2314],{"class":41,"line":290},[39,2313,302],{"class":45},[39,2315,2316],{"class":56}," (type) {\n",[39,2318,2319,2321,2324],{"class":41,"line":299},[39,2320,311],{"class":45},[39,2322,2323],{"class":60}," \"a\"",[39,2325,317],{"class":56},[39,2327,2328,2331,2334,2336,2339],{"class":41,"line":308},[39,2329,2330],{"class":45},"      let",[39,2332,2333],{"class":56}," result ",[39,2335,326],{"class":45},[39,2337,2338],{"class":60}," \"A result\"",[39,2340,332],{"class":56},[39,2342,2343,2345],{"class":41,"line":320},[39,2344,1898],{"class":45},[39,2346,2347],{"class":56}," result;\n",[39,2349,2350,2352,2355],{"class":41,"line":335},[39,2351,311],{"class":45},[39,2353,2354],{"class":60}," \"b\"",[39,2356,317],{"class":56},[39,2358,2359,2361,2363,2365,2368],{"class":41,"line":345},[39,2360,2330],{"class":45},[39,2362,2333],{"class":56},[39,2364,326],{"class":45},[39,2366,2367],{"class":60}," \"B result\"",[39,2369,332],{"class":56},[39,2371,2372,2374],{"class":41,"line":357},[39,2373,1898],{"class":45},[39,2375,2347],{"class":56},[39,2377,2378],{"class":41,"line":365},[39,2379,380],{"class":56},[39,2381,2382],{"class":41,"line":377},[39,2383,394],{"class":56},[93,2385,2387,2398,2417,2423],{"className":2386},[96],[98,2388,2390,2392,2393,2395,2396],{"className":2389},[101],[103,2391],{"disabled":105,"type":106}," This runs fine; each ",[23,2394,512],{}," gets its own scope for ",[23,2397,781],{},[98,2399,2401,107,2403,2406,2407,2409,2410,2412,2413,2416],{"className":2400},[101],[103,2402],{"disabled":105,"type":106},[23,2404,2405],{},"SyntaxError: Identifier 'result' has already been declared"," — a ",[23,2408,260],{}," block (without per-case ",[23,2411,154],{},") is a single shared block scope, so both ",[23,2414,2415],{},"let result"," declarations collide",[98,2418,2420,2422],{"className":2419},[101],[103,2421],{"disabled":105,"type":106}," Only a runtime error occurs when both cases are reached, not a parse-time error",[98,2424,2426,2428,2429,2431],{"className":2425},[101],[103,2427],{"disabled":105,"type":106}," This is valid; the second ",[23,2430,2415],{}," simply shadows the first",[169,2433,2434,2436,2446],{},[172,2435,174],{},[176,2437,2438,182,2440,2442,2443,2445],{},[179,2439,181],{},[23,2441,476],{},", since the whole ",[23,2444,260],{}," body is one shared block scope",[176,2447,2448,107,2450,2452,2453,2456,2457,2459,2460,2463,2464,2466,2467,2469,2470,2472,2473,2476,2477,2479,2480,2482],{},[179,2449,210],{},[179,2451,503],{}," an entire ",[23,2454,2455],{},"switch (...) { ... }"," statement is just a single block for scoping purposes, unless individual ",[23,2458,512],{},"s add their own ",[23,2461,2462],{},"{ }"," braces. Both ",[23,2465,2415],{}," declarations exist in that same shared scope, so this is a duplicate-declaration ",[23,2468,476],{}," caught at parse time — before the function even runs, regardless of which ",[23,2471,512],{}," would actually execute. The fix is wrapping each case body in its own block: ",[23,2474,2475],{},"case \"a\": { let result = ...; return result; }",". This is a common surprise for developers assuming each ",[23,2478,512],{}," is automatically its own scope like each iteration of a ",[23,2481,781],{},"-based loop is.",[14,2484,2485,2495,2537,2586],{"language":16},[18,2486,2488,2489,2491,2492,2494],{"id":2487},"q12-what-happens-when-break-is-used-outside-of-a-loop-or-switch-without-a-label","Q12. What happens when ",[23,2490,256],{}," is used outside of a loop or ",[23,2493,260],{},", without a label?",[28,2496,2497],{"language":16},[31,2498,2500],{"className":33,"code":2499,"language":16,"meta":35,"style":35},"function test() {\n  if (true) {\n    break;\n  }\n}\n",[23,2501,2502,2512,2522,2529,2533],{"__ignoreMap":35},[39,2503,2504,2506,2509],{"class":41,"line":42},[39,2505,273],{"class":45},[39,2507,2508],{"class":276}," test",[39,2510,2511],{"class":56},"() {\n",[39,2513,2514,2516,2518,2520],{"class":41,"line":290},[39,2515,1297],{"class":45},[39,2517,608],{"class":56},[39,2519,1478],{"class":49},[39,2521,287],{"class":56},[39,2523,2524,2527],{"class":41,"line":299},[39,2525,2526],{"class":45},"    break",[39,2528,332],{"class":56},[39,2530,2531],{"class":41,"line":308},[39,2532,380],{"class":56},[39,2534,2535],{"class":41,"line":320},[39,2536,394],{"class":56},[93,2538,2540,2551,2569,2577],{"className":2539},[96],[98,2541,2543,2545,2546,2548,2549],{"className":2542},[101],[103,2544],{"disabled":105,"type":106}," It simply exits the ",[23,2547,25],{}," block, like a scoped ",[23,2550,554],{},[98,2552,2554,107,2556,2559,2560,2562,2563,2565,2566,2568],{"className":2553},[101],[103,2555],{"disabled":105,"type":106},[23,2557,2558],{},"SyntaxError: Illegal break statement"," — unlabeled ",[23,2561,256],{}," is only valid inside a loop or ",[23,2564,260],{},", not inside a bare ",[23,2567,25],{}," block",[98,2570,2572,2574,2575],{"className":2571},[101],[103,2573],{"disabled":105,"type":106}," It exits the entire function, behaving like ",[23,2576,554],{},[98,2578,2580,2582,2583,2585],{"className":2579},[101],[103,2581],{"disabled":105,"type":106}," It throws a runtime ",[23,2584,1432],{}," when the function is called, but parses fine",[169,2587,2588,2590,2602],{},[172,2589,174],{},[176,2591,2592,182,2594,2596,2597,2599,2600],{},[179,2593,181],{},[23,2595,476],{},", since unlabeled ",[23,2598,256],{}," requires an enclosing loop or ",[23,2601,260],{},[176,2603,2604,107,2606,2608,2609,64,2611,64,2614,2617,2618,2620,2621,2623,2624,107,2627,2630],{},[179,2605,210],{},[23,2607,256],{}," (without a label) is only legal directly inside a loop (",[23,2610,605],{},[23,2612,2613],{},"while",[23,2615,2616],{},"do...while",") or a ",[23,2619,260],{}," statement — it cannot be used to exit an arbitrary block like a bare ",[23,2622,25],{},". This is caught at parse time, so the function never even becomes callable (ruling out D, which wrongly assumes it parses). A labeled ",[23,2625,2626],{},"break label;",[668,2628,2629],{},"can"," target an arbitrary labeled block\u002Fstatement (not just loops), but that requires an explicit label, which this example doesn't have.",[14,2632,2633,2640,2702,2745],{"language":16},[18,2634,2636,2637,2639],{"id":2635},"q13-what-is-the-output-of-a-dowhile-loop-when-the-condition-is-false-from-the-start","Q13. What is the output of a ",[23,2638,2616],{}," loop when the condition is false from the start?",[28,2641,2642],{"language":16},[31,2643,2645],{"className":33,"code":2644,"language":16,"meta":35,"style":35},"let i = 10;\ndo {\n  console.log(i);\n  i++;\n} while (i \u003C 5);\n",[23,2646,2647,2659,2666,2676,2685],{"__ignoreMap":35},[39,2648,2649,2651,2653,2655,2657],{"class":41,"line":42},[39,2650,781],{"class":45},[39,2652,800],{"class":56},[39,2654,326],{"class":45},[39,2656,352],{"class":49},[39,2658,332],{"class":56},[39,2660,2661,2664],{"class":41,"line":290},[39,2662,2663],{"class":45},"do",[39,2665,1348],{"class":56},[39,2667,2668,2671,2673],{"class":41,"line":299},[39,2669,2670],{"class":56},"  console.",[39,2672,403],{"class":276},[39,2674,2675],{"class":56},"(i);\n",[39,2677,2678,2681,2683],{"class":41,"line":308},[39,2679,2680],{"class":56},"  i",[39,2682,819],{"class":45},[39,2684,332],{"class":56},[39,2686,2687,2690,2692,2695,2697,2700],{"class":41,"line":320},[39,2688,2689],{"class":56},"} ",[39,2691,2613],{"class":45},[39,2693,2694],{"class":56}," (i ",[39,2696,810],{"class":45},[39,2698,2699],{"class":49}," 5",[39,2701,845],{"class":56},[93,2703,2705,2715,2726,2735],{"className":2704},[96],[98,2706,2708,2710,2711,2714],{"className":2707},[101],[103,2709],{"disabled":105,"type":106}," Nothing is logged, since the condition ",[23,2712,2713],{},"i \u003C 5"," is false immediately",[98,2716,2718,107,2720,2722,2723,2725],{"className":2717},[101],[103,2719],{"disabled":105,"type":106},[23,2721,467],{}," is logged exactly once, because ",[23,2724,2616],{}," always executes the body at least once before checking the condition",[98,2727,2729,2731,2732,2734],{"className":2728},[101],[103,2730],{"disabled":105,"type":106}," It loops forever, since ",[23,2733,929],{}," starts above the threshold",[98,2736,2738,107,2740,1394,2742,2744],{"className":2737},[101],[103,2739],{"disabled":105,"type":106},[23,2741,476],{},[23,2743,2616],{}," requires the condition to be true initially",[169,2746,2747,2749,2759],{},[172,2748,174],{},[176,2750,2751,182,2753,2755,2756,2758],{},[179,2752,181],{},[23,2754,467],{}," is logged once; ",[23,2757,2616],{}," guarantees at least one execution before checking",[176,2760,2761,2763,2764,2766,2767,2770,2771,2773,2774,2777,2778,2780,2781,2783,2784,2787,2788,2791,2792,2794,2795,2797,2798,2800],{},[179,2762,210],{}," Unlike a ",[23,2765,2613],{}," loop (which checks the condition ",[668,2768,2769],{},"before"," the first iteration and may never execute its body), ",[23,2772,2616],{}," checks the condition ",[668,2775,2776],{},"after"," running the body, guaranteeing at least one execution regardless of the initial condition. Here, ",[23,2779,467],{}," is logged, ",[23,2782,929],{}," becomes ",[23,2785,2786],{},"11",", then the condition ",[23,2789,2790],{},"11 \u003C 5"," is checked and is ",[23,2793,196],{},", so the loop stops after exactly one iteration. Option A describes what a ",[23,2796,2613],{}," loop would do in the same situation. This basic-vs-while distinction is the main reason to reach for ",[23,2799,2616],{}," — when you need \"run at least once, then maybe repeat\" semantics (e.g., prompting a user at least once).",[14,2802,2803,2810,2855,2910],{"language":16},[18,2804,2806,2807,2809],{"id":2805},"q14-what-is-the-idiomatic-and-safest-way-to-iterate-a-maps-entries-with-both-key-and-value","Q14. What is the idiomatic and safest way to iterate a ",[23,2808,716],{},"'s entries with both key and value?",[28,2811,2812],{"language":16},[31,2813,2815],{"className":33,"code":2814,"language":16,"meta":35,"style":35},"const inventory = new Map([[\"apples\", 10], [\"bananas\", 5]]);\n",[23,2816,2817],{"__ignoreMap":35},[39,2818,2819,2821,2824,2826,2829,2832,2835,2838,2840,2842,2845,2848,2850,2852],{"class":41,"line":42},[39,2820,46],{"class":45},[39,2822,2823],{"class":49}," inventory",[39,2825,53],{"class":45},[39,2827,2828],{"class":45}," new",[39,2830,2831],{"class":276}," Map",[39,2833,2834],{"class":56},"([[",[39,2836,2837],{"class":60},"\"apples\"",[39,2839,64],{"class":56},[39,2841,467],{"class":49},[39,2843,2844],{"class":56},"], [",[39,2846,2847],{"class":60},"\"bananas\"",[39,2849,64],{"class":56},[39,2851,2105],{"class":49},[39,2853,2854],{"class":56},"]]);\n",[93,2856,2858,2867,2886,2900],{"className":2857},[96],[98,2859,2861,107,2863,2866],{"className":2860},[101],[103,2862],{"disabled":105,"type":106},[23,2864,2865],{},"for (const item in inventory) console.log(item);"," — same as arrays",[98,2868,2870,107,2872,2875,2876,2878,2879,2882,2883,2885],{"className":2869},[101],[103,2871],{"disabled":105,"type":106},[23,2873,2874],{},"for (const [key, value] of inventory) console.log(key, value);"," — ",[23,2877,716],{}," is iterable and yields ",[23,2880,2881],{},"[key, value]"," pairs by default, which destructure cleanly in a ",[23,2884,567],{}," loop",[98,2887,2889,107,2891,2894,2895,2897,2898],{"className":2888},[101],[103,2890],{"disabled":105,"type":106},[23,2892,2893],{},"inventory.forEach((key, value) => ...)"," is the only correct approach; ",[23,2896,567],{}," doesn't work on ",[23,2899,716],{},[98,2901,2903,2905,2906,2909],{"className":2902},[101],[103,2904],{"disabled":105,"type":106}," Convert to an array first with ",[23,2907,2908],{},"Array.from(inventory.keys())"," before any iteration is possible",[169,2911,2912,2914,2927],{},[172,2913,174],{},[176,2915,2916,182,2918,1394,2921,2923,2924,2926],{},[179,2917,181],{},[23,2919,2920],{},"for (const [key, value] of inventory)",[23,2922,716],{}," yields ",[23,2925,2881],{}," pairs by default",[176,2928,2929,107,2931,107,2933,2935,2936,2938,2939,2941,2942,2944,2945,2947,2948,2951,2952,2955,2956,2958,2959,2961,2962,2964],{},[179,2930,210],{},[179,2932,762],{},[23,2934,716],{},"'s default iterator (used by ",[23,2937,567],{},") yields ",[23,2940,2881],{}," two-element arrays, which pair perfectly with array destructuring in the loop variable. Option A is wrong — ",[23,2943,564],{}," is for enumerable object properties, and ",[23,2946,716],{}," instances don't expose their entries that way (it would iterate nothing meaningful here). Option C is misleading — ",[23,2949,2950],{},"Map.prototype.forEach"," does exist and works, but note its callback argument order is ",[23,2953,2954],{},"(value, key)",", the reverse of the ",[23,2957,2881],{}," pairs from iteration — a frequent source of confusion; and ",[23,2960,567],{}," absolutely does work on ",[23,2963,716],{},", contrary to the claim. Option D is unnecessarily indirect.",[14,2966,2967,2974,3073,3121],{"language":16},[18,2968,2970,2971,2973],{"id":2969},"q15-what-is-the-idiomatic-way-to-avoid-switch-fallthrough-bugs-in-production-code","Q15. What is the idiomatic way to avoid ",[23,2972,260],{}," fallthrough bugs in production code?",[28,2975,2976],{"language":16},[31,2977,2979],{"className":33,"code":2978,"language":16,"meta":35,"style":35},"function getShippingCost(zone) {\n  switch (zone) {\n    case \"domestic\":\n      return 5;\n    case \"international\":\n      return 25;\n    default:\n      throw new Error(`Unknown zone: ${zone}`);\n  }\n}\n",[23,2980,2981,2995,3002,3011,3019,3028,3037,3043,3065,3069],{"__ignoreMap":35},[39,2982,2983,2985,2988,2990,2993],{"class":41,"line":42},[39,2984,273],{"class":45},[39,2986,2987],{"class":276}," getShippingCost",[39,2989,280],{"class":56},[39,2991,2992],{"class":283},"zone",[39,2994,287],{"class":56},[39,2996,2997,2999],{"class":41,"line":290},[39,2998,302],{"class":45},[39,3000,3001],{"class":56}," (zone) {\n",[39,3003,3004,3006,3009],{"class":41,"line":299},[39,3005,311],{"class":45},[39,3007,3008],{"class":60}," \"domestic\"",[39,3010,317],{"class":56},[39,3012,3013,3015,3017],{"class":41,"line":308},[39,3014,1898],{"class":45},[39,3016,2699],{"class":49},[39,3018,332],{"class":56},[39,3020,3021,3023,3026],{"class":41,"line":320},[39,3022,311],{"class":45},[39,3024,3025],{"class":60}," \"international\"",[39,3027,317],{"class":56},[39,3029,3030,3032,3035],{"class":41,"line":335},[39,3031,1898],{"class":45},[39,3033,3034],{"class":49}," 25",[39,3036,332],{"class":56},[39,3038,3039,3041],{"class":41,"line":345},[39,3040,360],{"class":45},[39,3042,317],{"class":56},[39,3044,3045,3048,3050,3053,3055,3058,3060,3063],{"class":41,"line":357},[39,3046,3047],{"class":45},"      throw",[39,3049,2828],{"class":45},[39,3051,3052],{"class":276}," Error",[39,3054,280],{"class":56},[39,3056,3057],{"class":60},"`Unknown zone: ${",[39,3059,2992],{"class":56},[39,3061,3062],{"class":60},"}`",[39,3064,845],{"class":56},[39,3066,3067],{"class":41,"line":365},[39,3068,380],{"class":56},[39,3070,3071],{"class":41,"line":377},[39,3072,394],{"class":56},[93,3074,3076,3092,3109,3115],{"className":3075},[96],[98,3077,3079,3081,3082,3084,3085,3088,3089,3091],{"className":3078},[101],[103,3080],{"disabled":105,"type":106}," Never use ",[23,3083,260],{},"; always use ",[23,3086,3087],{},"if\u002Felse if"," chains instead, since ",[23,3090,260],{}," is inherently unsafe",[98,3093,3095,3097,3098,3100,3101,1240,3103,3105,3106,3108],{"className":3094},[101],[103,3096],{"disabled":105,"type":106}," Always end every ",[23,3099,512],{}," with an explicit ",[23,3102,554],{},[23,3104,256],{}," (as shown), and always include a ",[23,3107,446],{}," case — ideally one that throws or logs on an unexpected value rather than silently doing nothing, to surface unhandled cases immediately",[98,3110,3112,3114],{"className":3111},[101],[103,3113],{"disabled":105,"type":106}," Fallthrough is rarely a real issue in practice, so no special precaution is needed",[98,3116,3118,3120],{"className":3117},[101],[103,3119],{"disabled":105,"type":106}," Rely on linters alone; no code-level pattern is necessary",[169,3122,3123,3125,3133],{},[172,3124,174],{},[176,3126,3127,3129,3130,3132],{},[179,3128,181],{}," B — Terminate every case explicitly and include a ",[23,3131,446],{}," that surfaces unexpected values (e.g., by throwing)",[176,3134,3135,107,3137,3139,3140,3142,3143,3145,3146,3148,3149,3151,3152,3154,3155,3158],{},[179,3136,210],{},[179,3138,762],{}," using ",[23,3141,554],{}," (inside a function, as shown) or ",[23,3144,256],{}," after every case body eliminates unintended fallthrough entirely, and — just as important — an explicit ",[23,3147,446],{}," branch that throws (rather than silently falling through to nothing) turns an unhandled case into a loud, immediate failure instead of a silent bug discovered much later. Option A overcorrects; ",[23,3150,260],{}," is fine and often more readable than long ",[23,3153,3087],{}," chains when used with this discipline. Option C dismisses a genuinely common bug class (Q2 of this file demonstrates exactly this). Option D is incomplete — linters (e.g., ",[23,3156,3157],{},"no-fallthrough",") are a good complementary safety net, but relying on tooling alone instead of also writing defensive code is weaker than doing both.",[14,3160,3161,3171,3272,3315],{"language":16},[18,3162,3164,3165,3167,3168,3170],{"id":3163},"q16-why-is-forof-generally-preferred-over-indexed-for-loops-when-iterating-arrays-in-modern-code-and-whats-the-tradeoff","Q16. Why is ",[23,3166,567],{}," generally preferred over indexed ",[23,3169,605],{}," loops when iterating arrays in modern code, and what's the tradeoff?",[28,3172,3173],{"language":16},[31,3174,3176],{"className":33,"code":3175,"language":16,"meta":35,"style":35},"const items = [\"a\", \"b\", \"c\"];\nfor (let i = 0; i \u003C items.length; i++) {\n  console.log(items[i]);\n}\nfor (const item of items) {\n  console.log(item);\n}\n",[23,3177,3178,3201,3230,3239,3243,3259,3268],{"__ignoreMap":35},[39,3179,3180,3182,3185,3187,3189,3191,3193,3195,3197,3199],{"class":41,"line":42},[39,3181,46],{"class":45},[39,3183,3184],{"class":49}," items",[39,3186,53],{"class":45},[39,3188,57],{"class":56},[39,3190,588],{"class":60},[39,3192,64],{"class":56},[39,3194,593],{"class":60},[39,3196,64],{"class":56},[39,3198,598],{"class":60},[39,3200,91],{"class":56},[39,3202,3203,3205,3207,3209,3211,3213,3215,3217,3219,3222,3224,3226,3228],{"class":41,"line":290},[39,3204,605],{"class":45},[39,3206,608],{"class":56},[39,3208,781],{"class":45},[39,3210,800],{"class":56},[39,3212,326],{"class":45},[39,3214,372],{"class":49},[39,3216,807],{"class":56},[39,3218,810],{"class":45},[39,3220,3221],{"class":56}," items.",[39,3223,1794],{"class":49},[39,3225,816],{"class":56},[39,3227,819],{"class":45},[39,3229,287],{"class":56},[39,3231,3232,3234,3236],{"class":41,"line":299},[39,3233,2670],{"class":56},[39,3235,403],{"class":276},[39,3237,3238],{"class":56},"(items[i]);\n",[39,3240,3241],{"class":41,"line":308},[39,3242,394],{"class":56},[39,3244,3245,3247,3249,3251,3254,3256],{"class":41,"line":320},[39,3246,605],{"class":45},[39,3248,608],{"class":56},[39,3250,46],{"class":45},[39,3252,3253],{"class":49}," item",[39,3255,637],{"class":45},[39,3257,3258],{"class":56}," items) {\n",[39,3260,3261,3263,3265],{"class":41,"line":335},[39,3262,2670],{"class":56},[39,3264,403],{"class":276},[39,3266,3267],{"class":56},"(item);\n",[39,3269,3270],{"class":41,"line":345},[39,3271,394],{"class":56},[93,3273,3275,3283,3294,3309],{"className":3274},[96],[98,3276,3278,107,3280,3282],{"className":3277},[101],[103,3279],{"disabled":105,"type":106},[23,3281,567],{}," is always strictly faster, so it should always be used",[98,3284,3286,107,3288,3290,3291,3293],{"className":3285},[101],[103,3287],{"disabled":105,"type":106},[23,3289,567],{}," is more concise and less error-prone (no manual index bookkeeping, no off-by-one risk), but the classic indexed ",[23,3292,605],{}," loop is still preferable when you need the index itself, need to skip\u002Fstep irregularly, or need to mutate the array while iterating",[98,3295,3297,3299,3300,3302,3303,3305,3306,3308],{"className":3296},[101],[103,3298],{"disabled":105,"type":106}," Indexed ",[23,3301,605],{}," loops cannot be used with ",[23,3304,46],{}," items, only ",[23,3307,567],{}," can",[98,3310,3312,3314],{"className":3311},[101],[103,3313],{"disabled":105,"type":106}," There's no meaningful tradeoff; they're fully interchangeable in every scenario",[169,3316,3317,3319,3326],{},[172,3318,174],{},[176,3320,3321,182,3323,3325],{},[179,3322,181],{},[23,3324,567],{}," is more concise and avoids index bugs, but indexed loops are still preferable when the index, custom stepping, or safe in-place mutation is needed",[176,3327,3328,107,3330,107,3332,3334,3335,3338,3339,3342,3343,3346,3347,3349,3350,3353],{},[179,3329,210],{},[179,3331,762],{},[23,3333,567],{}," removes an entire class of bugs tied to manual index management (off-by-one errors, forgetting ",[23,3336,3337],{},"i++",", wrong comparison operator). However, it doesn't expose the current index directly (use ",[23,3340,3341],{},"array.entries()"," with destructuring, ",[23,3344,3345],{},"for (const [i, item] of items.entries())",", if both are needed), and classic ",[23,3348,605],{}," loops remain the right tool for non-sequential stepping (",[23,3351,3352],{},"i += 2",") or careful in-place array mutation during iteration. Option A is an unfounded blanket performance claim — differences are typically negligible and engine\u002Fversion dependent. Option C is fabricated; both loop types work fine regardless of how the array variable itself was declared.",[14,3355,3356,3368,3449,3483],{"language":16},[18,3357,3359,3360,244,3362,3364,3365,3367],{"id":3358},"q17-what-is-the-best-practice-explanation-for-preferring-early-returncontinue-guard-clauses-over-deeply-nested-if-blocks","Q17. What is the best-practice explanation for preferring early ",[23,3361,554],{},[23,3363,1040],{}," guard clauses over deeply nested ",[23,3366,25],{}," blocks?",[28,3369,3370],{"language":16},[31,3371,3373],{"className":33,"code":3372,"language":16,"meta":35,"style":35},"function processOrder(order) {\n  if (order.isValid) {\n    if (order.inStock) {\n      if (order.paymentConfirmed) {\n        return shipOrder(order);\n      }\n    }\n  }\n  return null;\n}\n",[23,3374,3375,3389,3396,3403,3411,3422,3427,3432,3436,3445],{"__ignoreMap":35},[39,3376,3377,3379,3382,3384,3387],{"class":41,"line":42},[39,3378,273],{"class":45},[39,3380,3381],{"class":276}," processOrder",[39,3383,280],{"class":56},[39,3385,3386],{"class":283},"order",[39,3388,287],{"class":56},[39,3390,3391,3393],{"class":41,"line":290},[39,3392,1297],{"class":45},[39,3394,3395],{"class":56}," (order.isValid) {\n",[39,3397,3398,3400],{"class":41,"line":299},[39,3399,1112],{"class":45},[39,3401,3402],{"class":56}," (order.inStock) {\n",[39,3404,3405,3408],{"class":41,"line":308},[39,3406,3407],{"class":45},"      if",[39,3409,3410],{"class":56}," (order.paymentConfirmed) {\n",[39,3412,3413,3416,3419],{"class":41,"line":320},[39,3414,3415],{"class":45},"        return",[39,3417,3418],{"class":276}," shipOrder",[39,3420,3421],{"class":56},"(order);\n",[39,3423,3424],{"class":41,"line":335},[39,3425,3426],{"class":56},"      }\n",[39,3428,3429],{"class":41,"line":345},[39,3430,3431],{"class":56},"    }\n",[39,3433,3434],{"class":41,"line":357},[39,3435,380],{"class":56},[39,3437,3438,3440,3443],{"class":41,"line":365},[39,3439,386],{"class":45},[39,3441,3442],{"class":49}," null",[39,3444,332],{"class":56},[39,3446,3447],{"class":41,"line":377},[39,3448,394],{"class":56},[93,3450,3452,3458,3468,3474],{"className":3451},[96],[98,3453,3455,3457],{"className":3454},[101],[103,3456],{"disabled":105,"type":106}," Nested conditionals are always faster to execute, so this style should be kept for performance",[98,3459,3461,3463,3464,3467],{"className":3460},[101],[103,3462],{"disabled":105,"type":106}," Guard clauses (",[23,3465,3466],{},"if (!order.isValid) return null;"," etc., checked early and returned immediately) reduce nesting depth, keep the \"happy path\" flush against the left margin, and make each precondition's failure case easy to locate and reason about independently",[98,3469,3471,3473],{"className":3470},[101],[103,3472],{"disabled":105,"type":106}," Nesting depth has no effect on readability or maintainability; it's purely a stylistic preference with no practical benefit either way",[98,3475,3477,3479,3480,3482],{"className":3476},[101],[103,3478],{"disabled":105,"type":106}," Guard clauses are an anti-pattern because they create multiple ",[23,3481,554],{}," points in a function, which should always be avoided",[169,3484,3485,3487,3492],{},[172,3486,174],{},[176,3488,3489,3491],{},[179,3490,181],{}," B — Guard clauses reduce nesting, keep the happy path readable, and isolate each failure condition",[176,3493,3494,107,3496,3498,3499,3502,3503,3505],{},[179,3495,210],{},[179,3497,762],{}," rewriting the above as ",[23,3500,3501],{},"if (!order.isValid) return null; if (!order.inStock) return null; if (!order.paymentConfirmed) return null; return shipOrder(order);"," flattens three levels of nesting into a linear list of independently-readable preconditions — widely considered more maintainable, especially as more conditions are added over time (each nested ",[23,3504,25],{}," version requires increasing indentation for every check). Option A is false; nesting has no meaningful runtime performance implication either way — this is a readability\u002Fmaintainability concern, not a performance one. Option D states an outdated, overly rigid \"single return point\" rule that most modern style guides have moved away from in favor of guard clauses precisely because they improve clarity.",[14,3507,3508,3515,3598,3657],{"language":16},[18,3509,3511,3512,3514],{"id":3510},"q18-what-is-the-idiomatic-best-practice-regarding-forin-and-arrays-specifically","Q18. What is the idiomatic best practice regarding ",[23,3513,564],{}," and arrays specifically?",[28,3516,3517],{"language":16},[31,3518,3520],{"className":33,"code":3519,"language":16,"meta":35,"style":35},"Array.prototype.customHelper = function () {};\nconst nums = [1, 2, 3];\nfor (const key in nums) {\n  console.log(key);\n}\n",[23,3521,3522,3546,3569,3585,3594],{"__ignoreMap":35},[39,3523,3524,3527,3530,3533,3535,3538,3540,3543],{"class":41,"line":42},[39,3525,3526],{"class":49},"Array",[39,3528,3529],{"class":56},".",[39,3531,3532],{"class":49},"prototype",[39,3534,3529],{"class":56},[39,3536,3537],{"class":276},"customHelper",[39,3539,53],{"class":45},[39,3541,3542],{"class":45}," function",[39,3544,3545],{"class":56}," () {};\n",[39,3547,3548,3550,3553,3555,3557,3559,3561,3563,3565,3567],{"class":41,"line":290},[39,3549,46],{"class":45},[39,3551,3552],{"class":49}," nums",[39,3554,53],{"class":45},[39,3556,57],{"class":56},[39,3558,1239],{"class":49},[39,3560,64],{"class":56},[39,3562,1243],{"class":49},[39,3564,64],{"class":56},[39,3566,933],{"class":49},[39,3568,91],{"class":56},[39,3570,3571,3573,3575,3577,3580,3582],{"class":41,"line":299},[39,3572,605],{"class":45},[39,3574,608],{"class":56},[39,3576,46],{"class":45},[39,3578,3579],{"class":49}," key",[39,3581,616],{"class":45},[39,3583,3584],{"class":56}," nums) {\n",[39,3586,3587,3589,3591],{"class":41,"line":308},[39,3588,2670],{"class":56},[39,3590,403],{"class":276},[39,3592,3593],{"class":56},"(key);\n",[39,3595,3596],{"class":41,"line":320},[39,3597,394],{"class":56},[93,3599,3601,3609,3640,3648],{"className":3600},[96],[98,3602,3604,107,3606,3608],{"className":3603},[101],[103,3605],{"disabled":105,"type":106},[23,3607,564],{}," is the recommended way to iterate arrays because it's the oldest, most universally supported syntax",[98,3610,3612,3614,3615,3617,3618,3620,3621,3624,3625,64,3627,1694,3630,244,3633,244,3636,3639],{"className":3611},[101],[103,3613],{"disabled":105,"type":106}," Avoid ",[23,3616,564],{}," on arrays: it iterates enumerable keys including inherited ones (like ",[23,3619,3537],{}," added to ",[23,3622,3623],{},"Array.prototype"," here would also show up), doesn't guarantee numeric order across engines for non-standard cases, and yields string indices rather than values — prefer ",[23,3626,567],{},[23,3628,3629],{},".forEach()",[23,3631,3632],{},".map()",[23,3634,3635],{},".filter()",[23,3637,3638],{},".reduce()"," instead",[98,3641,3643,107,3645,3647],{"className":3642},[101],[103,3644],{"disabled":105,"type":106},[23,3646,564],{}," never includes inherited properties, so this concern doesn't apply",[98,3649,3651,3653,3654,3656],{"className":3650},[101],[103,3652],{"disabled":105,"type":106}," This throws a ",[23,3655,1432],{}," because you can't add custom methods to built-in prototypes",[169,3658,3659,3661,3669],{},[172,3660,174],{},[176,3662,3663,3665,3666,3668],{},[179,3664,181],{}," B — Avoid ",[23,3667,564],{}," on arrays due to inherited enumerable properties, ordering caveats, and index-not-value semantics",[176,3670,3671,107,3673,107,3675,3677,3678,3680,3681,3683,3684,3686,3687,3689,3690,3692],{},[179,3672,210],{},[179,3674,762],{},[23,3676,564],{}," walks the prototype chain for enumerable properties by design, meaning any enumerable property added to ",[23,3679,3623],{}," (as ",[23,3682,3537],{}," is here) would actually surface as a \"key\" alongside the numeric indices during iteration — a subtle and surprising bug if a library or polyfill augments a built-in prototype non-defensively. Combined with the index-vs-value confusion from Q3, this is why ",[23,3685,567],{}," or array iteration methods are the idiomatic default for arrays, reserving ",[23,3688,564],{}," for genuinely enumerating plain object keys. Option C is false — this exact \"leaking through the prototype chain\" behavior is ",[23,3691,564],{},"'s defining (and risky) characteristic. Option D is false — augmenting built-in prototypes is legal JavaScript (if generally discouraged practice), not an error.",[14,3694,3695,3699,3758,3812],{"language":16},[18,3696,3698],{"id":3697},"q19-given-asynchronous-work-inside-a-loop-what-is-the-best-practice-pattern-to-run-iterations-sequentially-waiting-for-each-to-finish-before-starting-the-next","Q19. Given asynchronous work inside a loop, what is the best-practice pattern to run iterations sequentially (waiting for each to finish before starting the next)?",[28,3700,3701],{"language":16},[31,3702,3704],{"className":33,"code":3703,"language":16,"meta":35,"style":35},"async function processAll(ids) {\n  for (const id of ids) {\n    await processOne(id);\n  }\n}\n",[23,3705,3706,3723,3739,3750,3754],{"__ignoreMap":35},[39,3707,3708,3711,3713,3716,3718,3721],{"class":41,"line":42},[39,3709,3710],{"class":45},"async",[39,3712,3542],{"class":45},[39,3714,3715],{"class":276}," processAll",[39,3717,280],{"class":56},[39,3719,3720],{"class":283},"ids",[39,3722,287],{"class":56},[39,3724,3725,3727,3729,3731,3734,3736],{"class":41,"line":290},[39,3726,1085],{"class":45},[39,3728,608],{"class":56},[39,3730,46],{"class":45},[39,3732,3733],{"class":49}," id",[39,3735,637],{"class":45},[39,3737,3738],{"class":56}," ids) {\n",[39,3740,3741,3744,3747],{"class":41,"line":299},[39,3742,3743],{"class":45},"    await",[39,3745,3746],{"class":276}," processOne",[39,3748,3749],{"class":56},"(id);\n",[39,3751,3752],{"class":41,"line":308},[39,3753,380],{"class":56},[39,3755,3756],{"class":41,"line":320},[39,3757,394],{"class":56},[93,3759,3761,3773,3797,3806],{"className":3760},[96],[98,3762,3764,3766,3767,3770,3771,2885],{"className":3763},[101],[103,3765],{"disabled":105,"type":106}," This is a bug — ",[23,3768,3769],{},"await"," cannot be used inside a ",[23,3772,567],{},[98,3774,3776,3778,3779,3781,3782,1240,3784,3786,3787,3789,3790,3792,3793,3796],{"className":3775},[101],[103,3777],{"disabled":105,"type":106}," This correctly processes items one at a time, in order, because ",[23,3780,3769],{}," inside a standard ",[23,3783,605],{},[23,3785,567],{}," loop pauses that iteration until the promise resolves before continuing to the next; using ",[23,3788,3629],{}," with an ",[23,3791,3710],{}," callback would NOT achieve this, since ",[23,3794,3795],{},"forEach"," ignores returned promises and fires all callbacks immediately",[98,3798,3800,107,3802,3805],{"className":3799},[101],[103,3801],{"disabled":105,"type":106},[23,3803,3804],{},"ids.forEach(async (id) => await processOne(id))"," is equivalent and preferred for readability",[98,3807,3809,3811],{"className":3808},[101],[103,3810],{"disabled":105,"type":106}," Both approaches are equally sequential; the choice is purely stylistic",[169,3813,3814,3816,3831],{},[172,3815,174],{},[176,3817,3818,182,3820,3822,3823,3825,3826,3822,3828,3830],{},[179,3819,181],{},[23,3821,567],{}," with ",[23,3824,3769],{}," correctly sequences the work; ",[23,3827,3629],{},[23,3829,3710],{}," does not",[176,3832,3833,107,3835,1015,3837,3839,3840,3842,3843,3845,3846,3848,3849,3852,3853,3855,3856,3859,3860,3862,3863,244,3865,244,3867,3869,3870,3873],{},[179,3834,210],{},[179,3836,503],{},[23,3838,567],{}," (or classic ",[23,3841,605],{},") loop with ",[23,3844,3769],{}," inside genuinely pauses the loop's execution at each ",[23,3847,3769],{},", so iterations run strictly one after another. ",[23,3850,3851],{},"Array.prototype.forEach",", by contrast, invokes its callback for every element immediately and synchronously, ignoring any promise the callback returns — marking the callback ",[23,3854,3710],{}," doesn't change this, it just means each callback silently returns an unhandled\u002Funawaited promise, so all the ",[23,3857,3858],{},"processOne"," calls actually fire concurrently, not sequentially, which is almost never the intended behavior when this mistake is made. Option A is false — ",[23,3861,3769],{}," works fine inside ",[23,3864,605],{},[23,3866,567],{},[23,3868,2613],{}," loops in an ",[23,3871,3872],{},"async function",". Option C is exactly the trap the explanation warns against.",[14,3875,3876,3884,3990,4028],{"language":16},[18,3877,3879,3880,3883],{"id":3878},"q20-in-error-prone-control-flow-involving-trycatch-inside-a-loop-what-is-the-best-practice-when-one-iterations-failure-shouldnt-stop-the-rest","Q20. In error-prone control flow involving ",[23,3881,3882],{},"try\u002Fcatch"," inside a loop, what is the best practice when one iteration's failure shouldn't stop the rest?",[28,3885,3886],{"language":16},[31,3887,3889],{"className":33,"code":3888,"language":16,"meta":35,"style":35},"function importAll(records) {\n  const errors = [];\n  for (const record of records) {\n    try {\n      saveRecord(record);\n    } catch (err) {\n      errors.push({ record, error: err.message });\n    }\n  }\n  return errors;\n}\n",[23,3890,3891,3905,3918,3934,3941,3949,3960,3971,3975,3979,3986],{"__ignoreMap":35},[39,3892,3893,3895,3898,3900,3903],{"class":41,"line":42},[39,3894,273],{"class":45},[39,3896,3897],{"class":276}," importAll",[39,3899,280],{"class":56},[39,3901,3902],{"class":283},"records",[39,3904,287],{"class":56},[39,3906,3907,3910,3913,3915],{"class":41,"line":290},[39,3908,3909],{"class":45},"  const",[39,3911,3912],{"class":49}," errors",[39,3914,53],{"class":45},[39,3916,3917],{"class":56}," [];\n",[39,3919,3920,3922,3924,3926,3929,3931],{"class":41,"line":299},[39,3921,1085],{"class":45},[39,3923,608],{"class":56},[39,3925,46],{"class":45},[39,3927,3928],{"class":49}," record",[39,3930,637],{"class":45},[39,3932,3933],{"class":56}," records) {\n",[39,3935,3936,3939],{"class":41,"line":308},[39,3937,3938],{"class":45},"    try",[39,3940,1348],{"class":56},[39,3942,3943,3946],{"class":41,"line":320},[39,3944,3945],{"class":276},"      saveRecord",[39,3947,3948],{"class":56},"(record);\n",[39,3950,3951,3954,3957],{"class":41,"line":335},[39,3952,3953],{"class":56},"    } ",[39,3955,3956],{"class":45},"catch",[39,3958,3959],{"class":56}," (err) {\n",[39,3961,3962,3965,3968],{"class":41,"line":345},[39,3963,3964],{"class":56},"      errors.",[39,3966,3967],{"class":276},"push",[39,3969,3970],{"class":56},"({ record, error: err.message });\n",[39,3972,3973],{"class":41,"line":357},[39,3974,3431],{"class":56},[39,3976,3977],{"class":41,"line":365},[39,3978,380],{"class":56},[39,3980,3981,3983],{"class":41,"line":377},[39,3982,386],{"class":45},[39,3984,3985],{"class":56}," errors;\n",[39,3987,3988],{"class":41,"line":383},[39,3989,394],{"class":56},[93,3991,3993,4002,4011,4019],{"className":3992},[96],[98,3994,3996,3998,3999,4001],{"className":3995},[101],[103,3997],{"disabled":105,"type":106}," Put the ",[23,4000,3882],{}," outside the loop entirely, wrapping the whole loop once",[98,4003,4005,4007,4008,4010],{"className":4004},[101],[103,4006],{"disabled":105,"type":106}," Put ",[23,4009,3882],{}," inside the loop body around just the risky operation (as shown), so a single record's failure is caught, recorded, and the loop continues to the next record rather than aborting the entire batch",[98,4012,4014,3081,4016,4018],{"className":4013},[101],[103,4015],{"disabled":105,"type":106},[23,4017,3882],{}," in loops; instead let the first error propagate and stop everything, since partial failures are always worse than total failure",[98,4020,4022,4024,4025,4027],{"className":4021},[101],[103,4023],{"disabled":105,"type":106}," Use ",[23,4026,3882],{}," outside the loop, but also inside — redundantly duplicating error handling at both levels",[169,4029,4030,4032,4040],{},[172,4031,174],{},[176,4033,4034,4036,4037,4039],{},[179,4035,181],{}," B — Place ",[23,4038,3882],{}," inside the loop around just the risky call so one failure doesn't abort the whole batch",[176,4041,4042,107,4044,4046,4047,4049,4050,4053,4054,4056,4057,4060,4061,4063],{},[179,4043,210],{},[179,4045,762],{}," wrapping the ",[23,4048,3882],{}," around only ",[23,4051,4052],{},"saveRecord(record)"," inside the loop means an exception on one iteration is caught, logged\u002Fcollected, and execution proceeds to the next record — appropriate for batch\u002Fbulk-import scenarios where partial success is valuable and one bad record shouldn't sink the whole batch. Option A is a common mistake: a ",[23,4055,3882],{}," wrapped around the entire loop instead means the ",[668,4058,4059],{},"first"," error thrown anywhere aborts the rest of the loop immediately (the ",[23,4062,3956],{}," block only runs once, and the loop never resumes), losing all remaining records' processing. Option C is an oversimplified rule that ignores real use cases (like bulk imports) where collecting partial results and continuing is the desired, correct behavior — not every error should be fatal to the whole operation.",[4065,4066,4067],"style",{},"html pre.shiki code .svdQ7, html code.shiki .svdQ7{--shiki-default:#D73A49;--shiki-github-dark:#F97583}html pre.shiki code .snvgF, html code.shiki .snvgF{--shiki-default:#005CC5;--shiki-github-dark:#79B8FF}html pre.shiki code .ssxIu, html code.shiki .ssxIu{--shiki-default:#24292E;--shiki-github-dark:#E1E4E8}html pre.shiki code .sJ6F3, html code.shiki .sJ6F3{--shiki-default:#032F62;--shiki-github-dark:#9ECBFF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .github-dark .shiki span {color: var(--shiki-github-dark);background: var(--shiki-github-dark-bg);font-style: var(--shiki-github-dark-font-style);font-weight: var(--shiki-github-dark-font-weight);text-decoration: var(--shiki-github-dark-text-decoration);}html.github-dark .shiki span {color: var(--shiki-github-dark);background: var(--shiki-github-dark-bg);font-style: var(--shiki-github-dark-font-style);font-weight: var(--shiki-github-dark-font-weight);text-decoration: var(--shiki-github-dark-text-decoration);}html pre.shiki code .sIsaT, html code.shiki .sIsaT{--shiki-default:#6F42C1;--shiki-github-dark:#B392F0}html pre.shiki code .sCrzJ, html code.shiki .sCrzJ{--shiki-default:#E36209;--shiki-github-dark:#FFAB70}",{"title":35,"searchDepth":290,"depth":290,"links":4069},[4070,4072,4074,4076,4078,4080,4082,4084,4086,4088,4090,4092,4094,4096,4098,4100,4102,4104,4106,4107],{"id":20,"depth":299,"text":4071},"Q1. Which of these values is falsy in a boolean context (e.g., inside an if)?",{"id":252,"depth":299,"text":4073},"Q2. What happens without break statements in a switch?",{"id":560,"depth":299,"text":4075},"Q3. What's the key difference between for...in and for...of?",{"id":773,"depth":299,"text":4077},"Q4. What is the classic closure-in-loop bug with var, and how does let fix it?",{"id":1036,"depth":299,"text":4079},"Q5. What does a labeled continue statement do inside nested loops?",{"id":1266,"depth":299,"text":4081},"Q6. What is the output of this if\u002Felse if\u002Felse chain, given JavaScript's truthy\u002Ffalsy coercion?",{"id":1512,"depth":299,"text":4083},"Q7. What does a for loop do when the condition is omitted entirely?",{"id":1687,"depth":299,"text":4085},"Q8. What happens when a for...of loop is used on a plain object (not an array, Map, or Set)?",{"id":1854,"depth":299,"text":4087},"Q9. What is the output when a switch uses strict comparison against mixed types?",{"id":2081,"depth":299,"text":4089},"Q10. What does an empty for loop body with a semicolon do?",{"id":2281,"depth":299,"text":4091},"Q11. What is the scoping behavior of a variable declared with let inside a switch block with no per-case braces?",{"id":2487,"depth":299,"text":4093},"Q12. What happens when break is used outside of a loop or switch, without a label?",{"id":2635,"depth":299,"text":4095},"Q13. What is the output of a do...while loop when the condition is false from the start?",{"id":2805,"depth":299,"text":4097},"Q14. What is the idiomatic and safest way to iterate a Map's entries with both key and value?",{"id":2969,"depth":299,"text":4099},"Q15. What is the idiomatic way to avoid switch fallthrough bugs in production code?",{"id":3163,"depth":299,"text":4101},"Q16. Why is for...of generally preferred over indexed for loops when iterating arrays in modern code, and what's the tradeoff?",{"id":3358,"depth":299,"text":4103},"Q17. What is the best-practice explanation for preferring early return\u002Fcontinue guard clauses over deeply nested if blocks?",{"id":3510,"depth":299,"text":4105},"Q18. What is the idiomatic best practice regarding for...in and arrays specifically?",{"id":3697,"depth":299,"text":3698},{"id":3878,"depth":299,"text":4108},"Q20. In error-prone control flow involving try\u002Fcatch inside a loop, what is the best practice when one iteration's failure shouldn't stop the rest?","md",{},"\u002Fjs\u002F04-control-flow",{"title":5,"description":35},"js\u002F04-control-flow","ZRiqqBfgrDeuJ9n6qFbntIsp9ZX3QAin1k4_wCnnjEE",1787335397332]