[{"data":1,"prerenderedAt":3588},["ShallowReactive",2],{"page-\u002Fjs\u002F05-functions-and-scope":3},{"id":4,"title":5,"body":6,"description":134,"extension":3582,"meta":3583,"navigation":34,"path":3584,"seo":3585,"stem":3586,"__hash__":3587},"content\u002Fjs\u002F05-functions-and-scope.md","05 — Functions & Scope",{"type":7,"value":8,"toc":3559},"minimark",[9,13,119,299,459,614,759,980,1193,1400,1567,1708,1868,2030,2276,2440,2644,2832,3023,3173,3324,3555],[10,11,5],"h1",{"id":12},"_05-functions-scope",[14,15,16,21,64],"question-wrapper",{},[17,18,20],"h3",{"id":19},"q1-which-statement-correctly-describes-the-difference-between-how-function-declarations-and-function-expressions-are-hoisted","Q1. Which statement correctly describes the difference between how function declarations and function expressions are hoisted?",[22,23,26,46,52,58],"ul",{"className":24},[25],"contains-task-list",[27,28,31,36,37,41,42,45],"li",{"className":29},[30],"task-list-item",[32,33],"input",{"disabled":34,"type":35},true,"checkbox"," Function declarations are hoisted complete with their implementation and can be called before their textual position; function expressions are only as hoisted as the variable they're assigned to (or not hoisted at all for ",[38,39,40],"code",{},"const","\u002F",[38,43,44],{},"let",")",[27,47,49,51],{"className":48},[30],[32,50],{"disabled":34,"type":35}," Neither declarations nor expressions are hoisted; both must appear in the source before they're used",[27,53,55,57],{"className":54},[30],[32,56],{"disabled":34,"type":35}," Both are fully hoisted with their implementation, so both can be called before their textual position in the code",[27,59,61,63],{"className":60},[30],[32,62],{"disabled":34,"type":35}," Function expressions are hoisted complete with their implementation, but declarations are not hoisted at all",[65,66,67,71,83],"details",{},[68,69,70],"summary",{},"Show Answer",[72,73,74,78,79,41,81,45],"p",{},[75,76,77],"strong",{},"Answer:"," A — Function declarations are hoisted complete with their implementation and can be called before their textual position; function expressions are only as hoisted as the variable they're assigned to (or not hoisted at all for ",[38,80,40],{},[38,82,44],{},[72,84,85,88,89,92,93,97,98,101,102,105,106,109,110,113,114,41,116,118],{},[75,86,87],{},"Explanation:"," During the creation phase, the JS engine hoists an entire ",[38,90,91],{},"function foo() {}"," declaration — name and body — to the top of its enclosing scope, so it's fully callable before its line in the source. A function ",[94,95,96],"em",{},"expression",", like ",[38,99,100],{},"const foo = function () {}"," or ",[38,103,104],{},"var foo = function () {}",", is just a variable assignment; only the variable binding follows that variable's own hoisting rules (",[38,107,108],{},"var"," → hoisted and ",[38,111,112],{},"undefined",", ",[38,115,44],{},[38,117,40],{}," → hoisted into the temporal dead zone), and the function value itself only exists once the assignment line runs. Options C and D describe hoisting behavior that doesn't apply to either form.",[14,120,122,126,210,256],{"language":121},"javascript",[17,123,125],{"id":124},"q2-what-does-this-log","Q2. What does this log?",[127,128,129],"code-wrapper",{"language":121},[130,131,135],"pre",{"className":132,"code":133,"language":121,"meta":134,"style":134},"language-javascript shiki shiki-themes github-light github-dark","console.log(typeof greet);\ngreet();\n\nfunction greet() {\n  console.log(\"hello from a declaration\");\n}\n","",[38,136,137,160,169,175,187,204],{"__ignoreMap":134},[138,139,142,146,150,153,157],"span",{"class":140,"line":141},"line",1,[138,143,145],{"class":144},"ssxIu","console.",[138,147,149],{"class":148},"sIsaT","log",[138,151,152],{"class":144},"(",[138,154,156],{"class":155},"svdQ7","typeof",[138,158,159],{"class":144}," greet);\n",[138,161,163,166],{"class":140,"line":162},2,[138,164,165],{"class":148},"greet",[138,167,168],{"class":144},"();\n",[138,170,172],{"class":140,"line":171},3,[138,173,174],{"emptyLinePlaceholder":34},"\n",[138,176,178,181,184],{"class":140,"line":177},4,[138,179,180],{"class":155},"function",[138,182,183],{"class":148}," greet",[138,185,186],{"class":144},"() {\n",[138,188,190,193,195,197,201],{"class":140,"line":189},5,[138,191,192],{"class":144},"  console.",[138,194,149],{"class":148},[138,196,152],{"class":144},[138,198,200],{"class":199},"sJ6F3","\"hello from a declaration\"",[138,202,203],{"class":144},");\n",[138,205,207],{"class":140,"line":206},6,[138,208,209],{"class":144},"}\n",[22,211,213,226,237,247],{"className":212},[25],[27,214,216,218,219,222,223],{"className":215},[30],[32,217],{"disabled":34,"type":35}," ",[38,220,221],{},"\"undefined\""," then a ",[38,224,225],{},"TypeError",[27,227,229,218,231,234,235],{"className":228},[30],[32,230],{"disabled":34,"type":35},[38,232,233],{},"\"function\""," then ",[38,236,200],{},[27,238,240,242,243,246],{"className":239},[30],[32,241],{"disabled":34,"type":35}," a ",[38,244,245],{},"ReferenceError"," before anything logs",[27,248,250,218,252,222,254],{"className":249},[30],[32,251],{"disabled":34,"type":35},[38,253,233],{},[38,255,245],{},[65,257,258,260,269],{},[68,259,70],{},[72,261,262,264,265,234,267],{},[75,263,77],{}," B — ",[38,266,233],{},[38,268,200],{},[72,270,271,273,274,276,277,280,281,284,285,287,288,291,292,294,295,298],{},[75,272,87],{}," Because ",[38,275,165],{}," is a function ",[94,278,279],{},"declaration",", both its name and its full implementation are hoisted to the top of the module\u002Fscript scope during the creation phase. By the time ",[38,282,283],{},"console.log(typeof greet)"," runs, ",[38,286,165],{}," already refers to a callable function, so ",[38,289,290],{},"typeof greet"," is ",[38,293,233],{},", and calling ",[38,296,297],{},"greet()"," immediately afterward executes normally even though the call appears above the declaration in the source.",[14,300,301,305,370,409],{"language":121},[17,302,304],{"id":303},"q3-what-does-this-log","Q3. What does this log?",[127,306,307],{"language":121},[130,308,310],{"className":132,"code":309,"language":121,"meta":134,"style":134},"console.log(typeof sayHi);\nsayHi();\n\nvar sayHi = function () {\n  console.log(\"hi from an expression\");\n};\n",[38,311,312,325,332,336,352,365],{"__ignoreMap":134},[138,313,314,316,318,320,322],{"class":140,"line":141},[138,315,145],{"class":144},[138,317,149],{"class":148},[138,319,152],{"class":144},[138,321,156],{"class":155},[138,323,324],{"class":144}," sayHi);\n",[138,326,327,330],{"class":140,"line":162},[138,328,329],{"class":148},"sayHi",[138,331,168],{"class":144},[138,333,334],{"class":140,"line":171},[138,335,174],{"emptyLinePlaceholder":34},[138,337,338,340,343,346,349],{"class":140,"line":177},[138,339,108],{"class":155},[138,341,342],{"class":148}," sayHi",[138,344,345],{"class":155}," =",[138,347,348],{"class":155}," function",[138,350,351],{"class":144}," () {\n",[138,353,354,356,358,360,363],{"class":140,"line":189},[138,355,192],{"class":144},[138,357,149],{"class":148},[138,359,152],{"class":144},[138,361,362],{"class":199},"\"hi from an expression\"",[138,364,203],{"class":144},[138,366,367],{"class":140,"line":206},[138,368,369],{"class":144},"};\n",[22,371,373,382,391,401],{"className":372},[25],[27,374,376,218,378,234,380],{"className":375},[30],[32,377],{"disabled":34,"type":35},[38,379,233],{},[38,381,362],{},[27,383,385,218,387,234,389],{"className":384},[30],[32,386],{"disabled":34,"type":35},[38,388,221],{},[38,390,362],{},[27,392,394,218,396,234,398],{"className":393},[30],[32,395],{"disabled":34,"type":35},[38,397,221],{},[38,399,400],{},"TypeError: sayHi is not a function",[27,402,404,218,406],{"className":403},[30],[32,405],{"disabled":34,"type":35},[38,407,408],{},"ReferenceError: sayHi is not defined",[65,410,411,413,422],{},[68,412,70],{},[72,414,415,417,418,234,420],{},[75,416,77],{}," C — ",[38,419,221],{},[38,421,400],{},[72,423,424,218,426,429,430,433,434,436,437,439,440,291,443,445,446,449,450,452,453,455,456,458],{},[75,425,87],{},[75,427,428],{},"Debug:"," only the ",[38,431,432],{},"var sayHi"," declaration is hoisted here, not the function it's assigned — ",[38,435,108],{}," bindings are hoisted and pre-initialized to ",[38,438,112],{},", so ",[38,441,442],{},"typeof sayHi",[38,444,221],{}," at the top. The actual function value isn't attached until the assignment line executes, so calling ",[38,447,448],{},"sayHi()"," before that line tries to invoke ",[38,451,112],{},", throwing a ",[38,454,225],{},". This is the key contrast with Q2: swapping a declaration for a ",[38,457,108],{},"-assigned expression turns \"works before its position\" into a runtime crash.",[14,460,461,465,531,573],{"language":121},[17,462,464],{"id":463},"q4-what-does-this-log","Q4. What does this log?",[127,466,467],{"language":121},[130,468,470],{"className":132,"code":469,"language":121,"meta":134,"style":134},"function reportStatus() {\n  console.log(status);\n  var status = \"pending\";\n  console.log(status);\n}\n\nreportStatus();\n",[38,471,472,481,490,507,515,519,523],{"__ignoreMap":134},[138,473,474,476,479],{"class":140,"line":141},[138,475,180],{"class":155},[138,477,478],{"class":148}," reportStatus",[138,480,186],{"class":144},[138,482,483,485,487],{"class":140,"line":162},[138,484,192],{"class":144},[138,486,149],{"class":148},[138,488,489],{"class":144},"(status);\n",[138,491,492,495,498,501,504],{"class":140,"line":171},[138,493,494],{"class":155},"  var",[138,496,497],{"class":144}," status ",[138,499,500],{"class":155},"=",[138,502,503],{"class":199}," \"pending\"",[138,505,506],{"class":144},";\n",[138,508,509,511,513],{"class":140,"line":177},[138,510,192],{"class":144},[138,512,149],{"class":148},[138,514,489],{"class":144},[138,516,517],{"class":140,"line":189},[138,518,209],{"class":144},[138,520,521],{"class":140,"line":206},[138,522,174],{"emptyLinePlaceholder":34},[138,524,526,529],{"class":140,"line":525},7,[138,527,528],{"class":148},"reportStatus",[138,530,168],{"class":144},[22,532,534,544,555,564],{"className":533},[25],[27,535,537,218,539,234,542],{"className":536},[30],[32,538],{"disabled":34,"type":35},[38,540,541],{},"\"pending\"",[38,543,541],{},[27,545,547,242,549,551,552],{"className":546},[30],[32,548],{"disabled":34,"type":35},[38,550,245],{}," on the first ",[38,553,554],{},"console.log",[27,556,558,218,560,234,562],{"className":557},[30],[32,559],{"disabled":34,"type":35},[38,561,112],{},[38,563,112],{},[27,565,567,218,569,234,571],{"className":566},[30],[32,568],{"disabled":34,"type":35},[38,570,112],{},[38,572,541],{},[65,574,575,577,586],{},[68,576,70],{},[72,578,579,581,582,234,584],{},[75,580,77],{}," D — ",[38,583,112],{},[38,585,541],{},[72,587,588,218,590,593,594,596,597,599,600,603,604,607,608,610,611,613],{},[75,589,87],{},[38,591,592],{},"var status"," is hoisted to the top of ",[38,595,528],{},"'s function body and initialized to ",[38,598,112],{}," before any code runs, so the first ",[38,601,602],{},"console.log(status)"," reads that placeholder value rather than throwing. Only the assignment ",[38,605,606],{},"status = \"pending\""," happens where it's textually written, so the second ",[38,609,554],{}," sees the updated value. Contrast this with Q5 below, where ",[38,612,44],{}," behaves very differently for the same shape of code.",[14,615,616,620,678,716],{"language":121},[17,617,619],{"id":618},"q5-what-does-this-log","Q5. What does this log?",[127,621,622],{"language":121},[130,623,625],{"className":132,"code":624,"language":121,"meta":134,"style":134},"function reportStatus() {\n  console.log(status);\n  let status = \"pending\";\n  console.log(status);\n}\n\nreportStatus();\n",[38,626,627,635,643,656,664,668,672],{"__ignoreMap":134},[138,628,629,631,633],{"class":140,"line":141},[138,630,180],{"class":155},[138,632,478],{"class":148},[138,634,186],{"class":144},[138,636,637,639,641],{"class":140,"line":162},[138,638,192],{"class":144},[138,640,149],{"class":148},[138,642,489],{"class":144},[138,644,645,648,650,652,654],{"class":140,"line":171},[138,646,647],{"class":155},"  let",[138,649,497],{"class":144},[138,651,500],{"class":155},[138,653,503],{"class":199},[138,655,506],{"class":144},[138,657,658,660,662],{"class":140,"line":177},[138,659,192],{"class":144},[138,661,149],{"class":148},[138,663,489],{"class":144},[138,665,666],{"class":140,"line":189},[138,667,209],{"class":144},[138,669,670],{"class":140,"line":206},[138,671,174],{"emptyLinePlaceholder":34},[138,673,674,676],{"class":140,"line":525},[138,675,528],{"class":148},[138,677,168],{"class":144},[22,679,681,689,698,707],{"className":680},[25],[27,682,684,218,686],{"className":683},[30],[32,685],{"disabled":34,"type":35},[38,687,688],{},"ReferenceError: Cannot access 'status' before initialization",[27,690,692,218,694,234,696],{"className":691},[30],[32,693],{"disabled":34,"type":35},[38,695,112],{},[38,697,541],{},[27,699,701,218,703,234,705],{"className":700},[30],[32,702],{"disabled":34,"type":35},[38,704,541],{},[38,706,541],{},[27,708,710,218,712,234,714],{"className":709},[30],[32,711],{"disabled":34,"type":35},[38,713,112],{},[38,715,112],{},[65,717,718,720,727],{},[68,719,70],{},[72,721,722,724,725],{},[75,723,77],{}," A — ",[38,726,688],{},[72,728,729,218,731,218,734,736,737,739,740,742,743,745,746,749,750,752,753,755,756,758],{},[75,730,87],{},[75,732,733],{},"Safety:",[38,735,44],{}," (and ",[38,738,40],{},") bindings are hoisted too, but unlike ",[38,741,108],{}," they are not initialized to ",[38,744,112],{}," — they sit in the \"temporal dead zone\" (TDZ) from the top of the block until their declaration line actually runs. Reading ",[38,747,748],{},"status"," anywhere in that window throws a ",[38,751,245],{}," rather than silently returning ",[38,754,112],{},". This is a deliberate safety improvement over ",[38,757,108],{},": it turns an easy-to-miss ordering bug into a loud, immediate failure.",[14,760,761,765,875,920],{"language":121},[17,762,764],{"id":763},"q6-what-does-this-log","Q6. What does this log?",[127,766,767],{"language":121},[130,768,770],{"className":132,"code":769,"language":121,"meta":134,"style":134},"const factorial = function calc(n) {\n  return n \u003C= 1 ? 1 : n * calc(n - 1);\n};\n\nconsole.log(factorial(5));\nconsole.log(typeof calc);\n",[38,771,772,795,835,839,843,862],{"__ignoreMap":134},[138,773,774,776,779,781,783,786,788,792],{"class":140,"line":141},[138,775,40],{"class":155},[138,777,778],{"class":148}," factorial",[138,780,345],{"class":155},[138,782,348],{"class":155},[138,784,785],{"class":148}," calc",[138,787,152],{"class":144},[138,789,791],{"class":790},"sCrzJ","n",[138,793,794],{"class":144},") {\n",[138,796,797,800,803,806,810,813,815,818,820,823,825,828,831,833],{"class":140,"line":162},[138,798,799],{"class":155},"  return",[138,801,802],{"class":144}," n ",[138,804,805],{"class":155},"\u003C=",[138,807,809],{"class":808},"snvgF"," 1",[138,811,812],{"class":155}," ?",[138,814,809],{"class":808},[138,816,817],{"class":155}," :",[138,819,802],{"class":144},[138,821,822],{"class":155},"*",[138,824,785],{"class":148},[138,826,827],{"class":144},"(n ",[138,829,830],{"class":155},"-",[138,832,809],{"class":808},[138,834,203],{"class":144},[138,836,837],{"class":140,"line":171},[138,838,369],{"class":144},[138,840,841],{"class":140,"line":177},[138,842,174],{"emptyLinePlaceholder":34},[138,844,845,847,849,851,854,856,859],{"class":140,"line":189},[138,846,145],{"class":144},[138,848,149],{"class":148},[138,850,152],{"class":144},[138,852,853],{"class":148},"factorial",[138,855,152],{"class":144},[138,857,858],{"class":808},"5",[138,860,861],{"class":144},"));\n",[138,863,864,866,868,870,872],{"class":140,"line":206},[138,865,145],{"class":144},[138,867,149],{"class":148},[138,869,152],{"class":144},[138,871,156],{"class":155},[138,873,874],{"class":144}," calc);\n",[22,876,878,888,897,909],{"className":877},[25],[27,879,881,218,883,234,886],{"className":880},[30],[32,882],{"disabled":34,"type":35},[38,884,885],{},"120",[38,887,233],{},[27,889,891,218,893,234,895],{"className":890},[30],[32,892],{"disabled":34,"type":35},[38,894,885],{},[38,896,221],{},[27,898,900,242,902,904,905,908],{"className":899},[30],[32,901],{"disabled":34,"type":35},[38,903,245],{}," on the ",[38,906,907],{},"factorial(5)"," call",[27,910,912,218,914,916,917],{"className":911},[30],[32,913],{"disabled":34,"type":35},[38,915,885],{}," then it throws ",[38,918,919],{},"ReferenceError: calc is not defined",[65,921,922,924,932],{},[68,923,70],{},[72,925,926,264,928,234,930],{},[75,927,77],{},[38,929,885],{},[38,931,221],{},[72,933,934,936,937,940,941,944,945,947,948,950,951,953,954,956,957,960,961,963,964,966,967,969,970,972,973,976,977,979],{},[75,935,87],{}," This is a ",[94,938,939],{},"named function expression",": the name ",[38,942,943],{},"calc"," is bound only inside the function's own body (useful for recursion), it is never added to the enclosing scope. So ",[38,946,907],{}," works fine and recurses via ",[38,949,943],{}," internally, correctly producing ",[38,952,885],{},". But outside, ",[38,955,943],{}," was never declared, so ",[38,958,959],{},"typeof calc"," safely evaluates to ",[38,962,221],{}," — ",[38,965,156],{}," never throws on an unresolvable identifier, it only returns ",[38,968,221],{},", unlike directly referencing ",[38,971,943],{}," (which ",[94,974,975],{},"would"," throw a ",[38,978,245],{},").",[14,981,982,986,1111,1146],{"language":121},[17,983,985],{"id":984},"q7-what-does-this-log","Q7. What does this log?",[127,987,988],{"language":121},[130,989,991],{"className":132,"code":990,"language":121,"meta":134,"style":134},"let compute = function run(n) {\n  if (n \u003C= 0) return 0;\n  return n + run(n - 1);\n};\n\nconst backup = compute;\ncompute = null;\n\nconsole.log(backup(3));\n",[38,992,993,1013,1036,1055,1059,1063,1075,1087,1092],{"__ignoreMap":134},[138,994,995,997,1000,1002,1004,1007,1009,1011],{"class":140,"line":141},[138,996,44],{"class":155},[138,998,999],{"class":148}," compute",[138,1001,345],{"class":155},[138,1003,348],{"class":155},[138,1005,1006],{"class":148}," run",[138,1008,152],{"class":144},[138,1010,791],{"class":790},[138,1012,794],{"class":144},[138,1014,1015,1018,1021,1023,1026,1029,1032,1034],{"class":140,"line":162},[138,1016,1017],{"class":155},"  if",[138,1019,1020],{"class":144}," (n ",[138,1022,805],{"class":155},[138,1024,1025],{"class":808}," 0",[138,1027,1028],{"class":144},") ",[138,1030,1031],{"class":155},"return",[138,1033,1025],{"class":808},[138,1035,506],{"class":144},[138,1037,1038,1040,1042,1045,1047,1049,1051,1053],{"class":140,"line":171},[138,1039,799],{"class":155},[138,1041,802],{"class":144},[138,1043,1044],{"class":155},"+",[138,1046,1006],{"class":148},[138,1048,827],{"class":144},[138,1050,830],{"class":155},[138,1052,809],{"class":808},[138,1054,203],{"class":144},[138,1056,1057],{"class":140,"line":177},[138,1058,369],{"class":144},[138,1060,1061],{"class":140,"line":189},[138,1062,174],{"emptyLinePlaceholder":34},[138,1064,1065,1067,1070,1072],{"class":140,"line":206},[138,1066,40],{"class":155},[138,1068,1069],{"class":808}," backup",[138,1071,345],{"class":155},[138,1073,1074],{"class":144}," compute;\n",[138,1076,1077,1080,1082,1085],{"class":140,"line":525},[138,1078,1079],{"class":144},"compute ",[138,1081,500],{"class":155},[138,1083,1084],{"class":808}," null",[138,1086,506],{"class":144},[138,1088,1090],{"class":140,"line":1089},8,[138,1091,174],{"emptyLinePlaceholder":34},[138,1093,1095,1097,1099,1101,1104,1106,1109],{"class":140,"line":1094},9,[138,1096,145],{"class":144},[138,1098,149],{"class":148},[138,1100,152],{"class":144},[138,1102,1103],{"class":148},"backup",[138,1105,152],{"class":144},[138,1107,1108],{"class":808},"3",[138,1110,861],{"class":144},[22,1112,1114,1122,1130,1138],{"className":1113},[25],[27,1115,1117,218,1119],{"className":1116},[30],[32,1118],{"disabled":34,"type":35},[38,1120,1121],{},"TypeError: run is not a function",[27,1123,1125,218,1127],{"className":1124},[30],[32,1126],{"disabled":34,"type":35},[38,1128,1129],{},"6",[27,1131,1133,218,1135],{"className":1132},[30],[32,1134],{"disabled":34,"type":35},[38,1136,1137],{},"null",[27,1139,1141,218,1143],{"className":1140},[30],[32,1142],{"disabled":34,"type":35},[38,1144,1145],{},"0",[65,1147,1148,1150,1156],{},[68,1149,70],{},[72,1151,1152,264,1154],{},[75,1153,77],{},[38,1155,1129],{},[72,1157,1158,273,1160,1163,1164,1167,1168,1171,1172,1174,1175,1178,1179,1181,1182,1184,1185,1188,1189,1192],{},[75,1159,87],{},[38,1161,1162],{},"compute"," is a ",[94,1165,1166],{},"named"," function expression, the recursive calls inside its body reference the stable internal name ",[38,1169,1170],{},"run",", not the outer ",[38,1173,1162],{}," variable. Reassigning ",[38,1176,1177],{},"compute = null"," has no effect on what ",[38,1180,1170],{}," points to inside the function — ",[38,1183,1170],{}," always refers back to the function itself. So ",[38,1186,1187],{},"backup(3)"," still recurses correctly: ",[38,1190,1191],{},"3 + 2 + 1 + 0 = 6",". This is exactly why named function expressions are the safer choice for recursion over relying on an outer, reassignable binding.",[14,1194,1195,1199,1312,1354],{"language":121},[17,1196,1198],{"id":1197},"q8-what-does-this-log","Q8. What does this log?",[127,1200,1201],{"language":121},[130,1202,1204],{"className":132,"code":1203,"language":121,"meta":134,"style":134},"let count = 10;\n\nfunction addCount(base, extra = count) {\n  return base + extra;\n}\n\nconsole.log(addCount(1));\ncount = 100;\nconsole.log(addCount(1));\n",[38,1205,1206,1220,1224,1246,1258,1262,1266,1284,1296],{"__ignoreMap":134},[138,1207,1208,1210,1213,1215,1218],{"class":140,"line":141},[138,1209,44],{"class":155},[138,1211,1212],{"class":144}," count ",[138,1214,500],{"class":155},[138,1216,1217],{"class":808}," 10",[138,1219,506],{"class":144},[138,1221,1222],{"class":140,"line":162},[138,1223,174],{"emptyLinePlaceholder":34},[138,1225,1226,1228,1231,1233,1236,1238,1241,1243],{"class":140,"line":171},[138,1227,180],{"class":155},[138,1229,1230],{"class":148}," addCount",[138,1232,152],{"class":144},[138,1234,1235],{"class":790},"base",[138,1237,113],{"class":144},[138,1239,1240],{"class":790},"extra",[138,1242,345],{"class":155},[138,1244,1245],{"class":144}," count) {\n",[138,1247,1248,1250,1253,1255],{"class":140,"line":177},[138,1249,799],{"class":155},[138,1251,1252],{"class":144}," base ",[138,1254,1044],{"class":155},[138,1256,1257],{"class":144}," extra;\n",[138,1259,1260],{"class":140,"line":189},[138,1261,209],{"class":144},[138,1263,1264],{"class":140,"line":206},[138,1265,174],{"emptyLinePlaceholder":34},[138,1267,1268,1270,1272,1274,1277,1279,1282],{"class":140,"line":525},[138,1269,145],{"class":144},[138,1271,149],{"class":148},[138,1273,152],{"class":144},[138,1275,1276],{"class":148},"addCount",[138,1278,152],{"class":144},[138,1280,1281],{"class":808},"1",[138,1283,861],{"class":144},[138,1285,1286,1289,1291,1294],{"class":140,"line":1089},[138,1287,1288],{"class":144},"count ",[138,1290,500],{"class":155},[138,1292,1293],{"class":808}," 100",[138,1295,506],{"class":144},[138,1297,1298,1300,1302,1304,1306,1308,1310],{"class":140,"line":1094},[138,1299,145],{"class":144},[138,1301,149],{"class":148},[138,1303,152],{"class":144},[138,1305,1276],{"class":148},[138,1307,152],{"class":144},[138,1309,1281],{"class":808},[138,1311,861],{"class":144},[22,1313,1315,1325,1335,1345],{"className":1314},[25],[27,1316,1318,218,1320,234,1323],{"className":1317},[30],[32,1319],{"disabled":34,"type":35},[38,1321,1322],{},"11",[38,1324,1322],{},[27,1326,1328,218,1330,234,1333],{"className":1327},[30],[32,1329],{"disabled":34,"type":35},[38,1331,1332],{},"101",[38,1334,1332],{},[27,1336,1338,218,1340,234,1343],{"className":1337},[30],[32,1339],{"disabled":34,"type":35},[38,1341,1342],{},"NaN",[38,1344,1342],{},[27,1346,1348,218,1350,234,1352],{"className":1347},[30],[32,1349],{"disabled":34,"type":35},[38,1351,1322],{},[38,1353,1332],{},[65,1355,1356,1358,1366],{},[68,1357,70],{},[72,1359,1360,581,1362,234,1364],{},[75,1361,77],{},[38,1363,1322],{},[38,1365,1332],{},[72,1367,1368,218,1370,1372,1373,1376,1377,1380,1381,1384,1385,1387,1388,1391,1392,1395,1396,1399],{},[75,1369,87],{},[75,1371,428],{}," a default parameter expression is not evaluated once and cached when the function is defined — it's re-evaluated fresh every time the function is called with that argument omitted. The first call reads the current ",[38,1374,1375],{},"count"," (",[38,1378,1379],{},"10","), giving ",[38,1382,1383],{},"1 + 10 = 11",". After ",[38,1386,1375],{}," is reassigned to ",[38,1389,1390],{},"100",", the second call re-evaluates ",[38,1393,1394],{},"extra = count"," and picks up the new value, giving ",[38,1397,1398],{},"1 + 100 = 101",". Treating default values as \"baked in once\" is a common but incorrect assumption.",[14,1401,1402,1406,1493,1528],{"language":121},[17,1403,1405],{"id":1404},"q9-what-does-this-log","Q9. What does this log?",[127,1407,1408],{"language":121},[130,1409,1411],{"className":132,"code":1410,"language":121,"meta":134,"style":134},"function createRange(start, end = start + 10, step = (end - start) \u002F 5) {\n  return { start, end, step };\n}\n\nconsole.log(createRange(0));\n",[38,1412,1413,1461,1468,1472,1476],{"__ignoreMap":134},[138,1414,1415,1417,1420,1422,1425,1427,1430,1432,1435,1437,1439,1441,1444,1446,1449,1451,1454,1456,1459],{"class":140,"line":141},[138,1416,180],{"class":155},[138,1418,1419],{"class":148}," createRange",[138,1421,152],{"class":144},[138,1423,1424],{"class":790},"start",[138,1426,113],{"class":144},[138,1428,1429],{"class":790},"end",[138,1431,345],{"class":155},[138,1433,1434],{"class":144}," start ",[138,1436,1044],{"class":155},[138,1438,1217],{"class":808},[138,1440,113],{"class":144},[138,1442,1443],{"class":790},"step",[138,1445,345],{"class":155},[138,1447,1448],{"class":144}," (end ",[138,1450,830],{"class":155},[138,1452,1453],{"class":144}," start) ",[138,1455,41],{"class":155},[138,1457,1458],{"class":808}," 5",[138,1460,794],{"class":144},[138,1462,1463,1465],{"class":140,"line":162},[138,1464,799],{"class":155},[138,1466,1467],{"class":144}," { start, end, step };\n",[138,1469,1470],{"class":140,"line":171},[138,1471,209],{"class":144},[138,1473,1474],{"class":140,"line":177},[138,1475,174],{"emptyLinePlaceholder":34},[138,1477,1478,1480,1482,1484,1487,1489,1491],{"class":140,"line":189},[138,1479,145],{"class":144},[138,1481,149],{"class":148},[138,1483,152],{"class":144},[138,1485,1486],{"class":148},"createRange",[138,1488,152],{"class":144},[138,1490,1145],{"class":808},[138,1492,861],{"class":144},[22,1494,1496,1504,1512,1520],{"className":1495},[25],[27,1497,1499,218,1501],{"className":1498},[30],[32,1500],{"disabled":34,"type":35},[38,1502,1503],{},"{ start: 0, end: 10, step: 2 }",[27,1505,1507,218,1509],{"className":1506},[30],[32,1508],{"disabled":34,"type":35},[38,1510,1511],{},"{ start: 0, end: undefined, step: NaN }",[27,1513,1515,218,1517],{"className":1514},[30],[32,1516],{"disabled":34,"type":35},[38,1518,1519],{},"ReferenceError: Cannot access 'end' before initialization",[27,1521,1523,218,1525],{"className":1522},[30],[32,1524],{"disabled":34,"type":35},[38,1526,1527],{},"{ start: 0, end: 10, step: undefined }",[65,1529,1530,1532,1538],{},[68,1531,70],{},[72,1533,1534,724,1536],{},[75,1535,77],{},[38,1537,1503],{},[72,1539,1540,1542,1543,1545,1546,1549,1550,1552,1553,1555,1556,1558,1559,1562,1563,1566],{},[75,1541,87],{}," Default parameters are evaluated left to right, and each one can freely reference any parameter to its left that has already been initialized. ",[38,1544,1429],{}," defaults to ",[38,1547,1548],{},"start + 10 = 10",", and by the time ",[38,1551,1443],{},"'s default runs, both ",[38,1554,1424],{}," and ",[38,1557,1429],{}," are already initialized, so ",[38,1560,1561],{},"step = (10 - 0) \u002F 5 = 2",". This only works in this left-to-right direction — see Q10 for what happens when a default tries to reference a parameter declared ",[94,1564,1565],{},"after"," it.",[14,1568,1569,1573,1645,1679],{"language":121},[17,1570,1572],{"id":1571},"q10-what-happens-when-this-runs","Q10. What happens when this runs?",[127,1574,1575],{"language":121},[130,1576,1578],{"className":132,"code":1577,"language":121,"meta":134,"style":134},"function build(a = b + 1, b = 5) {\n  return a + b;\n}\n\nconsole.log(build());\n",[38,1579,1580,1612,1623,1627,1631],{"__ignoreMap":134},[138,1581,1582,1584,1587,1589,1592,1594,1597,1599,1601,1603,1606,1608,1610],{"class":140,"line":141},[138,1583,180],{"class":155},[138,1585,1586],{"class":148}," build",[138,1588,152],{"class":144},[138,1590,1591],{"class":790},"a",[138,1593,345],{"class":155},[138,1595,1596],{"class":144}," b ",[138,1598,1044],{"class":155},[138,1600,809],{"class":808},[138,1602,113],{"class":144},[138,1604,1605],{"class":790},"b",[138,1607,345],{"class":155},[138,1609,1458],{"class":808},[138,1611,794],{"class":144},[138,1613,1614,1616,1618,1620],{"class":140,"line":162},[138,1615,799],{"class":155},[138,1617,242],{"class":144},[138,1619,1044],{"class":155},[138,1621,1622],{"class":144}," b;\n",[138,1624,1625],{"class":140,"line":171},[138,1626,209],{"class":144},[138,1628,1629],{"class":140,"line":177},[138,1630,174],{"emptyLinePlaceholder":34},[138,1632,1633,1635,1637,1639,1642],{"class":140,"line":189},[138,1634,145],{"class":144},[138,1636,149],{"class":148},[138,1638,152],{"class":144},[138,1640,1641],{"class":148},"build",[138,1643,1644],{"class":144},"());\n",[22,1646,1648,1656,1665,1672],{"className":1647},[25],[27,1649,1651,1653,1654],{"className":1650},[30],[32,1652],{"disabled":34,"type":35}," Logs ",[38,1655,1129],{},[27,1657,1659,1661,1662],{"className":1658},[30],[32,1660],{"disabled":34,"type":35}," Throws ",[38,1663,1664],{},"ReferenceError: Cannot access 'b' before initialization",[27,1666,1668,1653,1670],{"className":1667},[30],[32,1669],{"disabled":34,"type":35},[38,1671,1342],{},[27,1673,1675,1653,1677],{"className":1674},[30],[32,1676],{"disabled":34,"type":35},[38,1678,1322],{},[65,1680,1681,1683,1690],{},[68,1682,70],{},[72,1684,1685,1687,1688],{},[75,1686,77],{}," B — Throws ",[38,1689,1664],{},[72,1691,1692,218,1694,1696,1697,1699,1700,1702,1703,113,1705,1707],{},[75,1693,87],{},[75,1695,733],{}," parameters form their own scope, evaluated in declaration order, and each parameter sits in a temporal dead zone until its own initialization runs — just like ",[38,1698,44],{}," inside a block. When ",[38,1701,1591],{},"'s default tries to read ",[38,1704,1605],{},[38,1706,1605],{}," hasn't been initialized yet, so it's still in the TDZ and the read throws. Defaults are not hoisted or resolved out of order; they only work looking backward at already-initialized parameters (Q9), never forward.",[14,1709,1710,1714,1794,1829],{"language":121},[17,1711,1713],{"id":1712},"q11-what-does-this-log","Q11. What does this log?",[127,1715,1716],{"language":121},[130,1717,1719],{"className":132,"code":1718,"language":121,"meta":134,"style":134},"function connect(host, port = 8080) {\n  return `${host}:${port}`;\n}\n\nconsole.log(connect(\"db\", null));\n",[38,1720,1721,1745,1764,1768,1772],{"__ignoreMap":134},[138,1722,1723,1725,1728,1730,1733,1735,1738,1740,1743],{"class":140,"line":141},[138,1724,180],{"class":155},[138,1726,1727],{"class":148}," connect",[138,1729,152],{"class":144},[138,1731,1732],{"class":790},"host",[138,1734,113],{"class":144},[138,1736,1737],{"class":790},"port",[138,1739,345],{"class":155},[138,1741,1742],{"class":808}," 8080",[138,1744,794],{"class":144},[138,1746,1747,1749,1752,1754,1757,1759,1762],{"class":140,"line":162},[138,1748,799],{"class":155},[138,1750,1751],{"class":199}," `${",[138,1753,1732],{"class":144},[138,1755,1756],{"class":199},"}:${",[138,1758,1737],{"class":144},[138,1760,1761],{"class":199},"}`",[138,1763,506],{"class":144},[138,1765,1766],{"class":140,"line":171},[138,1767,209],{"class":144},[138,1769,1770],{"class":140,"line":177},[138,1771,174],{"emptyLinePlaceholder":34},[138,1773,1774,1776,1778,1780,1783,1785,1788,1790,1792],{"class":140,"line":189},[138,1775,145],{"class":144},[138,1777,149],{"class":148},[138,1779,152],{"class":144},[138,1781,1782],{"class":148},"connect",[138,1784,152],{"class":144},[138,1786,1787],{"class":199},"\"db\"",[138,1789,113],{"class":144},[138,1791,1137],{"class":808},[138,1793,861],{"class":144},[22,1795,1797,1805,1813,1821],{"className":1796},[25],[27,1798,1800,218,1802],{"className":1799},[30],[32,1801],{"disabled":34,"type":35},[38,1803,1804],{},"\"db:8080\"",[27,1806,1808,218,1810],{"className":1807},[30],[32,1809],{"disabled":34,"type":35},[38,1811,1812],{},"\"db:undefined\"",[27,1814,1816,218,1818],{"className":1815},[30],[32,1817],{"disabled":34,"type":35},[38,1819,1820],{},"\"db:null\"",[27,1822,1824,218,1826],{"className":1823},[30],[32,1825],{"disabled":34,"type":35},[38,1827,1828],{},"TypeError: cannot use null as port",[65,1830,1831,1833,1839],{},[68,1832,70],{},[72,1834,1835,417,1837],{},[75,1836,77],{},[38,1838,1820],{},[72,1840,1841,218,1843,1845,1846,1848,1849,1851,1852,1854,1855,1857,1858,1861,1862,1864,1865,1867],{},[75,1842,87],{},[75,1844,428],{}," default parameters only kick in when the argument is exactly ",[38,1847,112],{}," (including simply being omitted) — not for any other \"empty-ish\" value. ",[38,1850,1137],{}," is a perfectly valid, explicitly passed value, so ",[38,1853,1737],{}," is set to ",[38,1856,1137],{}," and the default ",[38,1859,1860],{},"8080"," is never used; the template literal then stringifies ",[38,1863,1137],{}," to ",[38,1866,1820],{},". This is a common trap for anyone assuming defaults behave like a general null-coalescing fallback.",[14,1869,1870,1874,1952,1986],{"language":121},[17,1871,1873],{"id":1872},"q12-what-does-this-log","Q12. What does this log?",[127,1875,1876],{"language":121},[130,1877,1879],{"className":132,"code":1878,"language":121,"meta":134,"style":134},"function createTimer(label, retries = 3) {\n  return `${label} retries=${retries}`;\n}\n\nconsole.log(createTimer(\"job\", 0));\n",[38,1880,1881,1905,1922,1926,1930],{"__ignoreMap":134},[138,1882,1883,1885,1888,1890,1893,1895,1898,1900,1903],{"class":140,"line":141},[138,1884,180],{"class":155},[138,1886,1887],{"class":148}," createTimer",[138,1889,152],{"class":144},[138,1891,1892],{"class":790},"label",[138,1894,113],{"class":144},[138,1896,1897],{"class":790},"retries",[138,1899,345],{"class":155},[138,1901,1902],{"class":808}," 3",[138,1904,794],{"class":144},[138,1906,1907,1909,1911,1913,1916,1918,1920],{"class":140,"line":162},[138,1908,799],{"class":155},[138,1910,1751],{"class":199},[138,1912,1892],{"class":144},[138,1914,1915],{"class":199},"} retries=${",[138,1917,1897],{"class":144},[138,1919,1761],{"class":199},[138,1921,506],{"class":144},[138,1923,1924],{"class":140,"line":171},[138,1925,209],{"class":144},[138,1927,1928],{"class":140,"line":177},[138,1929,174],{"emptyLinePlaceholder":34},[138,1931,1932,1934,1936,1938,1941,1943,1946,1948,1950],{"class":140,"line":189},[138,1933,145],{"class":144},[138,1935,149],{"class":148},[138,1937,152],{"class":144},[138,1939,1940],{"class":148},"createTimer",[138,1942,152],{"class":144},[138,1944,1945],{"class":199},"\"job\"",[138,1947,113],{"class":144},[138,1949,1145],{"class":808},[138,1951,861],{"class":144},[22,1953,1955,1963,1971,1978],{"className":1954},[25],[27,1956,1958,218,1960],{"className":1957},[30],[32,1959],{"disabled":34,"type":35},[38,1961,1962],{},"\"job retries=3\"",[27,1964,1966,218,1968],{"className":1965},[30],[32,1967],{"disabled":34,"type":35},[38,1969,1970],{},"\"job retries=NaN\"",[27,1972,1974,218,1976],{"className":1973},[30],[32,1975],{"disabled":34,"type":35},[38,1977,225],{},[27,1979,1981,218,1983],{"className":1980},[30],[32,1982],{"disabled":34,"type":35},[38,1984,1985],{},"\"job retries=0\"",[65,1987,1988,1990,1996],{},[68,1989,70],{},[72,1991,1992,581,1994],{},[75,1993,77],{},[38,1995,1985],{},[72,1997,1998,218,2000,2002,2003,2005,2006,2008,2009,2011,2012,2015,2016,2018,2019,2021,2022,2025,2026,2029],{},[75,1999,87],{},[75,2001,428],{}," default parameters check specifically for ",[38,2004,112],{},", not general falsiness. ",[38,2007,1145],{}," is a legitimate, explicitly passed argument, so it's kept as-is and ",[38,2010,1108],{}," is never substituted. This is the crucial difference from the common ",[38,2013,2014],{},"retries = retries || 3"," fallback idiom, which ",[94,2017,975],{}," incorrectly replace a passed ",[38,2020,1145],{}," (or ",[38,2023,2024],{},"\"\"",", or ",[38,2027,2028],{},"false",") with the fallback — a classic source of bugs when zero is a meaningful value.",[14,2031,2032,2036,2193,2225],{"language":121},[17,2033,2035],{"id":2034},"q13-what-does-this-log","Q13. What does this log?",[127,2037,2038],{"language":121},[130,2039,2041],{"className":132,"code":2040,"language":121,"meta":134,"style":134},"let calls = 0;\nfunction nextId() {\n  calls += 1;\n  return calls;\n}\n\nfunction createUser(name, id = nextId()) {\n  return { name, id };\n}\n\ncreateUser(\"alice\", 5);\ncreateUser(\"bob\");\ncreateUser(\"carol\", 9);\n\nconsole.log(calls);\n",[38,2042,2043,2056,2065,2077,2084,2088,2092,2116,2123,2127,2132,2149,2161,2178,2183],{"__ignoreMap":134},[138,2044,2045,2047,2050,2052,2054],{"class":140,"line":141},[138,2046,44],{"class":155},[138,2048,2049],{"class":144}," calls ",[138,2051,500],{"class":155},[138,2053,1025],{"class":808},[138,2055,506],{"class":144},[138,2057,2058,2060,2063],{"class":140,"line":162},[138,2059,180],{"class":155},[138,2061,2062],{"class":148}," nextId",[138,2064,186],{"class":144},[138,2066,2067,2070,2073,2075],{"class":140,"line":171},[138,2068,2069],{"class":144},"  calls ",[138,2071,2072],{"class":155},"+=",[138,2074,809],{"class":808},[138,2076,506],{"class":144},[138,2078,2079,2081],{"class":140,"line":177},[138,2080,799],{"class":155},[138,2082,2083],{"class":144}," calls;\n",[138,2085,2086],{"class":140,"line":189},[138,2087,209],{"class":144},[138,2089,2090],{"class":140,"line":206},[138,2091,174],{"emptyLinePlaceholder":34},[138,2093,2094,2096,2099,2101,2104,2106,2109,2111,2113],{"class":140,"line":525},[138,2095,180],{"class":155},[138,2097,2098],{"class":148}," createUser",[138,2100,152],{"class":144},[138,2102,2103],{"class":790},"name",[138,2105,113],{"class":144},[138,2107,2108],{"class":790},"id",[138,2110,345],{"class":155},[138,2112,2062],{"class":148},[138,2114,2115],{"class":144},"()) {\n",[138,2117,2118,2120],{"class":140,"line":1089},[138,2119,799],{"class":155},[138,2121,2122],{"class":144}," { name, id };\n",[138,2124,2125],{"class":140,"line":1094},[138,2126,209],{"class":144},[138,2128,2130],{"class":140,"line":2129},10,[138,2131,174],{"emptyLinePlaceholder":34},[138,2133,2135,2138,2140,2143,2145,2147],{"class":140,"line":2134},11,[138,2136,2137],{"class":148},"createUser",[138,2139,152],{"class":144},[138,2141,2142],{"class":199},"\"alice\"",[138,2144,113],{"class":144},[138,2146,858],{"class":808},[138,2148,203],{"class":144},[138,2150,2152,2154,2156,2159],{"class":140,"line":2151},12,[138,2153,2137],{"class":148},[138,2155,152],{"class":144},[138,2157,2158],{"class":199},"\"bob\"",[138,2160,203],{"class":144},[138,2162,2164,2166,2168,2171,2173,2176],{"class":140,"line":2163},13,[138,2165,2137],{"class":148},[138,2167,152],{"class":144},[138,2169,2170],{"class":199},"\"carol\"",[138,2172,113],{"class":144},[138,2174,2175],{"class":808},"9",[138,2177,203],{"class":144},[138,2179,2181],{"class":140,"line":2180},14,[138,2182,174],{"emptyLinePlaceholder":34},[138,2184,2186,2188,2190],{"class":140,"line":2185},15,[138,2187,145],{"class":144},[138,2189,149],{"class":148},[138,2191,2192],{"class":144},"(calls);\n",[22,2194,2196,2203,2210,2217],{"className":2195},[25],[27,2197,2199,218,2201],{"className":2198},[30],[32,2200],{"disabled":34,"type":35},[38,2202,1281],{},[27,2204,2206,218,2208],{"className":2205},[30],[32,2207],{"disabled":34,"type":35},[38,2209,1108],{},[27,2211,2213,218,2215],{"className":2212},[30],[32,2214],{"disabled":34,"type":35},[38,2216,1145],{},[27,2218,2220,218,2222],{"className":2219},[30],[32,2221],{"disabled":34,"type":35},[38,2223,2224],{},"2",[65,2226,2227,2229,2235],{},[68,2228,70],{},[72,2230,2231,724,2233],{},[75,2232,77],{},[38,2234,1281],{},[72,2236,2237,2239,2240,2243,2244,2246,2247,1555,2250,2253,2254,439,2256,2258,2259,2262,2263,2265,2266,2268,2269,2272,2273,2275],{},[75,2238,87],{}," A default parameter expression — including a function call like ",[38,2241,2242],{},"nextId()"," — only runs when its argument is actually omitted (or explicitly ",[38,2245,112],{},"). ",[38,2248,2249],{},"createUser(\"alice\", 5)",[38,2251,2252],{},"createUser(\"carol\", 9)"," both pass an explicit ",[38,2255,2108],{},[38,2257,2242],{}," never executes for them. Only ",[38,2260,2261],{},"createUser(\"bob\")"," omits ",[38,2264,2108],{},", triggering exactly one call to ",[38,2267,2242],{},", leaving ",[38,2270,2271],{},"calls"," at ",[38,2274,1281],{},". This side-effect timing is easy to get wrong if you assume the default expression runs on every invocation regardless of whether it's needed.",[14,2277,2278,2282,2372,2413],{"language":121},[17,2279,2281],{"id":2280},"q14-what-does-this-log","Q14. What does this log?",[127,2283,2284],{"language":121},[130,2285,2287],{"className":132,"code":2286,"language":121,"meta":134,"style":134},"function addItem(item, cart = []) {\n  cart.push(item);\n  return cart;\n}\n\nconsole.log(addItem(\"apple\"));\nconsole.log(addItem(\"banana\"));\n",[38,2288,2289,2311,2322,2329,2333,2337,2355],{"__ignoreMap":134},[138,2290,2291,2293,2296,2298,2301,2303,2306,2308],{"class":140,"line":141},[138,2292,180],{"class":155},[138,2294,2295],{"class":148}," addItem",[138,2297,152],{"class":144},[138,2299,2300],{"class":790},"item",[138,2302,113],{"class":144},[138,2304,2305],{"class":790},"cart",[138,2307,345],{"class":155},[138,2309,2310],{"class":144}," []) {\n",[138,2312,2313,2316,2319],{"class":140,"line":162},[138,2314,2315],{"class":144},"  cart.",[138,2317,2318],{"class":148},"push",[138,2320,2321],{"class":144},"(item);\n",[138,2323,2324,2326],{"class":140,"line":171},[138,2325,799],{"class":155},[138,2327,2328],{"class":144}," cart;\n",[138,2330,2331],{"class":140,"line":177},[138,2332,209],{"class":144},[138,2334,2335],{"class":140,"line":189},[138,2336,174],{"emptyLinePlaceholder":34},[138,2338,2339,2341,2343,2345,2348,2350,2353],{"class":140,"line":206},[138,2340,145],{"class":144},[138,2342,149],{"class":148},[138,2344,152],{"class":144},[138,2346,2347],{"class":148},"addItem",[138,2349,152],{"class":144},[138,2351,2352],{"class":199},"\"apple\"",[138,2354,861],{"class":144},[138,2356,2357,2359,2361,2363,2365,2367,2370],{"class":140,"line":525},[138,2358,145],{"class":144},[138,2360,149],{"class":148},[138,2362,152],{"class":144},[138,2364,2347],{"class":148},[138,2366,152],{"class":144},[138,2368,2369],{"class":199},"\"banana\"",[138,2371,861],{"class":144},[22,2373,2375,2386,2396,2405],{"className":2374},[25],[27,2376,2378,218,2380,234,2383],{"className":2377},[30],[32,2379],{"disabled":34,"type":35},[38,2381,2382],{},"[\"apple\"]",[38,2384,2385],{},"[\"apple\", \"banana\"]",[27,2387,2389,218,2391,234,2393],{"className":2388},[30],[32,2390],{"disabled":34,"type":35},[38,2392,2382],{},[38,2394,2395],{},"[\"banana\"]",[27,2397,2399,218,2401,234,2403],{"className":2398},[30],[32,2400],{"disabled":34,"type":35},[38,2402,2385],{},[38,2404,2385],{},[27,2406,2408,218,2410],{"className":2407},[30],[32,2409],{"disabled":34,"type":35},[38,2411,2412],{},"TypeError: cart is not extensible",[65,2414,2415,2417,2425],{},[68,2416,70],{},[72,2418,2419,264,2421,234,2423],{},[75,2420,77],{},[38,2422,2382],{},[38,2424,2395],{},[72,2426,2427,218,2429,2432,2433,2436,2437,2439],{},[75,2428,87],{},[75,2430,2431],{},"Idiom:"," unlike Python's infamous mutable-default-argument footgun (where a default ",[38,2434,2435],{},"list"," is created once and reused across every call), JavaScript re-evaluates a default parameter expression fresh every time it's needed. Each call that omits ",[38,2438,2305],{}," gets its own brand-new empty array — nothing accumulates or leaks between calls. Option A is exactly the Python-style behavior a learner might expect, but it does not happen in JavaScript.",[14,2441,2442,2446,2554,2587],{"language":121},[17,2443,2445],{"id":2444},"q15-what-happens-when-this-runs","Q15. What happens when this runs?",[127,2447,2448],{"language":121},[130,2449,2451],{"className":132,"code":2450,"language":121,"meta":134,"style":134},"function sumAll() {\n  return arguments.filter(n => n > 0).reduce((a, b) => a + b, 0);\n}\n\nconsole.log(sumAll(1, -2, 3));\n",[38,2452,2453,2462,2519,2523,2527],{"__ignoreMap":134},[138,2454,2455,2457,2460],{"class":140,"line":141},[138,2456,180],{"class":155},[138,2458,2459],{"class":148}," sumAll",[138,2461,186],{"class":144},[138,2463,2464,2466,2469,2472,2475,2477,2479,2482,2484,2487,2489,2491,2494,2497,2499,2501,2503,2505,2508,2510,2512,2515,2517],{"class":140,"line":162},[138,2465,799],{"class":155},[138,2467,2468],{"class":808}," arguments",[138,2470,2471],{"class":144},".",[138,2473,2474],{"class":148},"filter",[138,2476,152],{"class":144},[138,2478,791],{"class":790},[138,2480,2481],{"class":155}," =>",[138,2483,802],{"class":144},[138,2485,2486],{"class":155},">",[138,2488,1025],{"class":808},[138,2490,979],{"class":144},[138,2492,2493],{"class":148},"reduce",[138,2495,2496],{"class":144},"((",[138,2498,1591],{"class":790},[138,2500,113],{"class":144},[138,2502,1605],{"class":790},[138,2504,1028],{"class":144},[138,2506,2507],{"class":155},"=>",[138,2509,242],{"class":144},[138,2511,1044],{"class":155},[138,2513,2514],{"class":144}," b, ",[138,2516,1145],{"class":808},[138,2518,203],{"class":144},[138,2520,2521],{"class":140,"line":171},[138,2522,209],{"class":144},[138,2524,2525],{"class":140,"line":177},[138,2526,174],{"emptyLinePlaceholder":34},[138,2528,2529,2531,2533,2535,2538,2540,2542,2544,2546,2548,2550,2552],{"class":140,"line":189},[138,2530,145],{"class":144},[138,2532,149],{"class":148},[138,2534,152],{"class":144},[138,2536,2537],{"class":148},"sumAll",[138,2539,152],{"class":144},[138,2541,1281],{"class":808},[138,2543,113],{"class":144},[138,2545,830],{"class":155},[138,2547,2224],{"class":808},[138,2549,113],{"class":144},[138,2551,1108],{"class":808},[138,2553,861],{"class":144},[22,2555,2557,2565,2572,2580],{"className":2556},[25],[27,2558,2560,1653,2562],{"className":2559},[30],[32,2561],{"disabled":34,"type":35},[38,2563,2564],{},"4",[27,2566,2568,1653,2570],{"className":2567},[30],[32,2569],{"disabled":34,"type":35},[38,2571,1342],{},[27,2573,2575,218,2577],{"className":2574},[30],[32,2576],{"disabled":34,"type":35},[38,2578,2579],{},"TypeError: arguments.filter is not a function",[27,2581,2583,1653,2585],{"className":2582},[30],[32,2584],{"disabled":34,"type":35},[38,2586,1281],{},[65,2588,2589,2591,2597],{},[68,2590,70],{},[72,2592,2593,417,2595],{},[75,2594,77],{},[38,2596,2579],{},[72,2598,2599,218,2601,218,2603,291,2606,2609,2610,2613,2614,2617,2618,2621,2622,101,2624,2626,2627,2630,2631,2633,2634,101,2637,2640,2641,2643],{},[75,2600,87],{},[75,2602,428],{},[38,2604,2605],{},"arguments",[94,2607,2608],{},"array-like"," — it has a ",[38,2611,2612],{},"length"," and indexed properties — but it is not an actual ",[38,2615,2616],{},"Array"," instance, so it doesn't inherit ",[38,2619,2620],{},"Array.prototype"," methods like ",[38,2623,2474],{},[38,2625,2493],{},". Calling ",[38,2628,2629],{},".filter"," directly on it throws a ",[38,2632,225],{},". To use array methods you'd need to convert it first (",[38,2635,2636],{},"Array.from(arguments)",[38,2638,2639],{},"[...arguments]","), or better yet, avoid ",[38,2642,2605],{}," entirely in favor of a rest parameter (see Q17), which is a real array from the start.",[14,2645,2646,2650,2745,2777],{"language":121},[17,2647,2649],{"id":2648},"q16-what-does-this-log","Q16. What does this log?",[127,2651,2652],{"language":121},[130,2653,2655],{"className":132,"code":2654,"language":121,"meta":134,"style":134},"function outer() {\n  const inner = () => {\n    console.log(arguments[0]);\n  };\n  inner(\"z\");\n}\n\nouter(\"a\", \"b\");\n",[38,2656,2657,2666,2684,2703,2708,2720,2724,2728],{"__ignoreMap":134},[138,2658,2659,2661,2664],{"class":140,"line":141},[138,2660,180],{"class":155},[138,2662,2663],{"class":148}," outer",[138,2665,186],{"class":144},[138,2667,2668,2671,2674,2676,2679,2681],{"class":140,"line":162},[138,2669,2670],{"class":155},"  const",[138,2672,2673],{"class":148}," inner",[138,2675,345],{"class":155},[138,2677,2678],{"class":144}," () ",[138,2680,2507],{"class":155},[138,2682,2683],{"class":144}," {\n",[138,2685,2686,2689,2691,2693,2695,2698,2700],{"class":140,"line":171},[138,2687,2688],{"class":144},"    console.",[138,2690,149],{"class":148},[138,2692,152],{"class":144},[138,2694,2605],{"class":808},[138,2696,2697],{"class":144},"[",[138,2699,1145],{"class":808},[138,2701,2702],{"class":144},"]);\n",[138,2704,2705],{"class":140,"line":177},[138,2706,2707],{"class":144},"  };\n",[138,2709,2710,2713,2715,2718],{"class":140,"line":189},[138,2711,2712],{"class":148},"  inner",[138,2714,152],{"class":144},[138,2716,2717],{"class":199},"\"z\"",[138,2719,203],{"class":144},[138,2721,2722],{"class":140,"line":206},[138,2723,209],{"class":144},[138,2725,2726],{"class":140,"line":525},[138,2727,174],{"emptyLinePlaceholder":34},[138,2729,2730,2733,2735,2738,2740,2743],{"class":140,"line":1089},[138,2731,2732],{"class":148},"outer",[138,2734,152],{"class":144},[138,2736,2737],{"class":199},"\"a\"",[138,2739,113],{"class":144},[138,2741,2742],{"class":199},"\"b\"",[138,2744,203],{"class":144},[22,2746,2748,2755,2762,2770],{"className":2747},[25],[27,2749,2751,218,2753],{"className":2750},[30],[32,2752],{"disabled":34,"type":35},[38,2754,2717],{},[27,2756,2758,218,2760],{"className":2757},[30],[32,2759],{"disabled":34,"type":35},[38,2761,112],{},[27,2763,2765,218,2767],{"className":2764},[30],[32,2766],{"disabled":34,"type":35},[38,2768,2769],{},"ReferenceError: arguments is not defined",[27,2771,2773,218,2775],{"className":2772},[30],[32,2774],{"disabled":34,"type":35},[38,2776,2737],{},[65,2778,2779,2781,2787],{},[68,2780,70],{},[72,2782,2783,581,2785],{},[75,2784,77],{},[38,2786,2737],{},[72,2788,2789,218,2791,2793,2794,2796,2797,2799,2800,2803,2804,2806,2807,2809,2810,2812,2813,439,2816,291,2819,2821,2822,2824,2825,2828,2829,2831],{},[75,2790,87],{},[75,2792,428],{}," arrow functions never get their own ",[38,2795,2605],{}," object. Any ",[38,2798,2605],{}," reference inside an arrow function is resolved lexically, walking up to the nearest enclosing ",[94,2801,2802],{},"regular"," (non-arrow) function's ",[38,2805,2605],{}," — here, ",[38,2808,2732],{},"'s. ",[38,2811,2732],{}," was called with ",[38,2814,2815],{},"(\"a\", \"b\")",[38,2817,2818],{},"arguments[0]",[38,2820,2737],{},"; the ",[38,2823,2717],{}," passed directly to ",[38,2826,2827],{},"inner"," is irrelevant because ",[38,2830,2827],{}," never captures its own arguments at all. This is a frequent source of confusion when refactoring a regular function into an arrow function.",[14,2833,2834,2838,2942,2974],{"language":121},[17,2835,2837],{"id":2836},"q17-what-does-this-log","Q17. What does this log?",[127,2839,2840],{"language":121},[130,2841,2843],{"className":132,"code":2842,"language":121,"meta":134,"style":134},"function sumAll(...nums) {\n  return nums.filter(n => n > 0).reduce((a, b) => a + b, 0);\n}\n\nconsole.log(sumAll(1, -2, 3));\n",[38,2844,2845,2861,2908,2912,2916],{"__ignoreMap":134},[138,2846,2847,2849,2851,2853,2856,2859],{"class":140,"line":141},[138,2848,180],{"class":155},[138,2850,2459],{"class":148},[138,2852,152],{"class":144},[138,2854,2855],{"class":155},"...",[138,2857,2858],{"class":790},"nums",[138,2860,794],{"class":144},[138,2862,2863,2865,2868,2870,2872,2874,2876,2878,2880,2882,2884,2886,2888,2890,2892,2894,2896,2898,2900,2902,2904,2906],{"class":140,"line":162},[138,2864,799],{"class":155},[138,2866,2867],{"class":144}," nums.",[138,2869,2474],{"class":148},[138,2871,152],{"class":144},[138,2873,791],{"class":790},[138,2875,2481],{"class":155},[138,2877,802],{"class":144},[138,2879,2486],{"class":155},[138,2881,1025],{"class":808},[138,2883,979],{"class":144},[138,2885,2493],{"class":148},[138,2887,2496],{"class":144},[138,2889,1591],{"class":790},[138,2891,113],{"class":144},[138,2893,1605],{"class":790},[138,2895,1028],{"class":144},[138,2897,2507],{"class":155},[138,2899,242],{"class":144},[138,2901,1044],{"class":155},[138,2903,2514],{"class":144},[138,2905,1145],{"class":808},[138,2907,203],{"class":144},[138,2909,2910],{"class":140,"line":171},[138,2911,209],{"class":144},[138,2913,2914],{"class":140,"line":177},[138,2915,174],{"emptyLinePlaceholder":34},[138,2917,2918,2920,2922,2924,2926,2928,2930,2932,2934,2936,2938,2940],{"class":140,"line":189},[138,2919,145],{"class":144},[138,2921,149],{"class":148},[138,2923,152],{"class":144},[138,2925,2537],{"class":148},[138,2927,152],{"class":144},[138,2929,1281],{"class":808},[138,2931,113],{"class":144},[138,2933,830],{"class":155},[138,2935,2224],{"class":808},[138,2937,113],{"class":144},[138,2939,1108],{"class":808},[138,2941,861],{"class":144},[22,2943,2945,2952,2960,2967],{"className":2944},[25],[27,2946,2948,218,2950],{"className":2947},[30],[32,2949],{"disabled":34,"type":35},[38,2951,2564],{},[27,2953,2955,218,2957],{"className":2954},[30],[32,2956],{"disabled":34,"type":35},[38,2958,2959],{},"TypeError: nums.filter is not a function",[27,2961,2963,218,2965],{"className":2962},[30],[32,2964],{"disabled":34,"type":35},[38,2966,1342],{},[27,2968,2970,218,2972],{"className":2969},[30],[32,2971],{"disabled":34,"type":35},[38,2973,2224],{},[65,2975,2976,2978,2984],{},[68,2977,70],{},[72,2979,2980,724,2982],{},[75,2981,77],{},[38,2983,2564],{},[72,2985,2986,218,2988,2990,2991,2994,2995,439,2997,1555,2999,3001,3002,3005,3006,1555,3008,3010,3011,3013,3014,3016,3017,3019,3020,3022],{},[75,2987,87],{},[75,2989,2431],{}," a rest parameter (",[38,2992,2993],{},"...nums",") collects the remaining arguments into a genuine ",[38,2996,2616],{},[38,2998,2474],{},[38,3000,2493],{}," work directly with no conversion step — ",[38,3003,3004],{},"filter(n => n > 0)"," keeps ",[38,3007,1281],{},[38,3009,1108],{},", and ",[38,3012,2493],{}," sums them to ",[38,3015,2564],{},". This is exactly why rest parameters are the recommended, more readable replacement for the legacy ",[38,3018,2605],{}," object (contrast with Q15, where the equivalent code using ",[38,3021,2605],{}," throws).",[14,3024,3025,3033,3100,3131],{"language":121},[17,3026,3028,3029,3032],{"id":3027},"q18-what-is-requestlength-in-this-snippet","Q18. What is ",[38,3030,3031],{},"request.length"," in this snippet?",[127,3034,3035],{"language":121},[130,3036,3038],{"className":132,"code":3037,"language":121,"meta":134,"style":134},"function request(url, method = \"GET\", headers = {}) {\n  return method;\n}\n\nconsole.log(request.length);\n",[38,3039,3040,3072,3079,3083,3087],{"__ignoreMap":134},[138,3041,3042,3044,3047,3049,3052,3054,3057,3059,3062,3064,3067,3069],{"class":140,"line":141},[138,3043,180],{"class":155},[138,3045,3046],{"class":148}," request",[138,3048,152],{"class":144},[138,3050,3051],{"class":790},"url",[138,3053,113],{"class":144},[138,3055,3056],{"class":790},"method",[138,3058,345],{"class":155},[138,3060,3061],{"class":199}," \"GET\"",[138,3063,113],{"class":144},[138,3065,3066],{"class":790},"headers",[138,3068,345],{"class":155},[138,3070,3071],{"class":144}," {}) {\n",[138,3073,3074,3076],{"class":140,"line":162},[138,3075,799],{"class":155},[138,3077,3078],{"class":144}," method;\n",[138,3080,3081],{"class":140,"line":171},[138,3082,209],{"class":144},[138,3084,3085],{"class":140,"line":177},[138,3086,174],{"emptyLinePlaceholder":34},[138,3088,3089,3091,3093,3096,3098],{"class":140,"line":189},[138,3090,145],{"class":144},[138,3092,149],{"class":148},[138,3094,3095],{"class":144},"(request.",[138,3097,2612],{"class":808},[138,3099,203],{"class":144},[22,3101,3103,3110,3117,3124],{"className":3102},[25],[27,3104,3106,218,3108],{"className":3105},[30],[32,3107],{"disabled":34,"type":35},[38,3109,1108],{},[27,3111,3113,218,3115],{"className":3112},[30],[32,3114],{"disabled":34,"type":35},[38,3116,1281],{},[27,3118,3120,218,3122],{"className":3119},[30],[32,3121],{"disabled":34,"type":35},[38,3123,2224],{},[27,3125,3127,218,3129],{"className":3126},[30],[32,3128],{"disabled":34,"type":35},[38,3130,1145],{},[65,3132,3133,3135,3141],{},[68,3134,70],{},[72,3136,3137,264,3139],{},[75,3138,77],{},[38,3140,1281],{},[72,3142,3143,218,3145,218,3147,3150,3151,3154,3155,1555,3157,3159,3160,3162,3163,3165,3166,3168,3169,3172],{},[75,3144,87],{},[75,3146,428],{},[38,3148,3149],{},"Function.prototype.length"," only counts the parameters that appear ",[94,3152,3153],{},"before"," the first one with a default value (and rest parameters are never counted either). Here ",[38,3156,3056],{},[38,3158,3066],{}," both have defaults, so counting stops immediately after ",[38,3161,3051],{},", giving a length of ",[38,3164,1281],{}," — not ",[38,3167,1108],{},", even though the function accepts up to three arguments. This surprises people who expect ",[38,3170,3171],{},".length"," to reflect the full parameter list; it's really reporting the number of \"required-looking\" leading parameters.",[14,3174,3175,3179,3253,3286],{"language":121},[17,3176,3178],{"id":3177},"q19-this-entire-file-is-in-strict-mode-what-does-it-log","Q19. This entire file is in strict mode. What does it log?",[127,3180,3181],{"language":121},[130,3182,3184],{"className":132,"code":3183,"language":121,"meta":134,"style":134},"\"use strict\";\n\nif (true) {\n  function greet() {\n    return \"hi\";\n  }\n}\n\nconsole.log(typeof greet);\n",[38,3185,3186,3193,3197,3209,3218,3228,3233,3237,3241],{"__ignoreMap":134},[138,3187,3188,3191],{"class":140,"line":141},[138,3189,3190],{"class":199},"\"use strict\"",[138,3192,506],{"class":144},[138,3194,3195],{"class":140,"line":162},[138,3196,174],{"emptyLinePlaceholder":34},[138,3198,3199,3202,3204,3207],{"class":140,"line":171},[138,3200,3201],{"class":155},"if",[138,3203,1376],{"class":144},[138,3205,3206],{"class":808},"true",[138,3208,794],{"class":144},[138,3210,3211,3214,3216],{"class":140,"line":177},[138,3212,3213],{"class":155},"  function",[138,3215,183],{"class":148},[138,3217,186],{"class":144},[138,3219,3220,3223,3226],{"class":140,"line":189},[138,3221,3222],{"class":155},"    return",[138,3224,3225],{"class":199}," \"hi\"",[138,3227,506],{"class":144},[138,3229,3230],{"class":140,"line":206},[138,3231,3232],{"class":144},"  }\n",[138,3234,3235],{"class":140,"line":525},[138,3236,209],{"class":144},[138,3238,3239],{"class":140,"line":1089},[138,3240,174],{"emptyLinePlaceholder":34},[138,3242,3243,3245,3247,3249,3251],{"class":140,"line":1094},[138,3244,145],{"class":144},[138,3246,149],{"class":148},[138,3248,152],{"class":144},[138,3250,156],{"class":155},[138,3252,159],{"class":144},[22,3254,3256,3263,3271,3278],{"className":3255},[25],[27,3257,3259,218,3261],{"className":3258},[30],[32,3260],{"disabled":34,"type":35},[38,3262,233],{},[27,3264,3266,218,3268],{"className":3265},[30],[32,3267],{"disabled":34,"type":35},[38,3269,3270],{},"\"string\"",[27,3272,3274,218,3276],{"className":3273},[30],[32,3275],{"disabled":34,"type":35},[38,3277,221],{},[27,3279,3281,3283,3284],{"className":3280},[30],[32,3282],{"disabled":34,"type":35}," it throws a ",[38,3285,245],{},[65,3287,3288,3290,3296],{},[68,3289,70],{},[72,3291,3292,417,3294],{},[75,3293,77],{},[38,3295,221],{},[72,3297,3298,218,3300,3303,3304,3306,3307,3309,3310,3312,3313,3315,3316,736,3318,3320,3321,3323],{},[75,3299,87],{},[75,3301,3302],{},"Portability:"," in strict mode, the ECMAScript spec requires a function declaration inside a block (like this ",[38,3305,3201],{},") to be properly block-scoped, behaving essentially like ",[38,3308,44],{}," — it does not exist outside the ",[38,3311,3201],{}," block. So ",[38,3314,290],{}," outside is ",[38,3317,221],{},[38,3319,156],{}," never throws on an unresolvable identifier, ruling out option D). In legacy non-strict (\"sloppy\") code, engines instead apply Annex B compatibility semantics, which additionally leak the function's name into the enclosing function\u002Fglobal scope initialized to ",[38,3322,112],{}," — a long-standing, easy-to-forget inconsistency between strict and sloppy mode that's best avoided by not declaring functions inside blocks at all.",[14,3325,3326,3330,3448,3505],{"language":121},[17,3327,3329],{"id":3328},"q20-what-does-this-log-and-what-is-the-pattern-accomplishing","Q20. What does this log, and what is the pattern accomplishing?",[127,3331,3332],{"language":121},[130,3333,3335],{"className":132,"code":3334,"language":121,"meta":134,"style":134},"const cache = {};\n\n(function initCache() {\n  const secretSalt = \"xyz123\";\n  cache.hash = str => str.length + secretSalt.length;\n})();\n\nconsole.log(typeof secretSalt);\nconsole.log(cache.hash(\"abc\"));\n",[38,3336,3337,3349,3353,3364,3378,3408,3413,3417,3430],{"__ignoreMap":134},[138,3338,3339,3341,3344,3346],{"class":140,"line":141},[138,3340,40],{"class":155},[138,3342,3343],{"class":808}," cache",[138,3345,345],{"class":155},[138,3347,3348],{"class":144}," {};\n",[138,3350,3351],{"class":140,"line":162},[138,3352,174],{"emptyLinePlaceholder":34},[138,3354,3355,3357,3359,3362],{"class":140,"line":171},[138,3356,152],{"class":144},[138,3358,180],{"class":155},[138,3360,3361],{"class":148}," initCache",[138,3363,186],{"class":144},[138,3365,3366,3368,3371,3373,3376],{"class":140,"line":177},[138,3367,2670],{"class":155},[138,3369,3370],{"class":808}," secretSalt",[138,3372,345],{"class":155},[138,3374,3375],{"class":199}," \"xyz123\"",[138,3377,506],{"class":144},[138,3379,3380,3383,3386,3388,3391,3393,3396,3398,3401,3404,3406],{"class":140,"line":189},[138,3381,3382],{"class":144},"  cache.",[138,3384,3385],{"class":148},"hash",[138,3387,345],{"class":155},[138,3389,3390],{"class":790}," str",[138,3392,2481],{"class":155},[138,3394,3395],{"class":144}," str.",[138,3397,2612],{"class":808},[138,3399,3400],{"class":155}," +",[138,3402,3403],{"class":144}," secretSalt.",[138,3405,2612],{"class":808},[138,3407,506],{"class":144},[138,3409,3410],{"class":140,"line":206},[138,3411,3412],{"class":144},"})();\n",[138,3414,3415],{"class":140,"line":525},[138,3416,174],{"emptyLinePlaceholder":34},[138,3418,3419,3421,3423,3425,3427],{"class":140,"line":1089},[138,3420,145],{"class":144},[138,3422,149],{"class":148},[138,3424,152],{"class":144},[138,3426,156],{"class":155},[138,3428,3429],{"class":144}," secretSalt);\n",[138,3431,3432,3434,3436,3439,3441,3443,3446],{"class":140,"line":1094},[138,3433,145],{"class":144},[138,3435,149],{"class":148},[138,3437,3438],{"class":144},"(cache.",[138,3440,3385],{"class":148},[138,3442,152],{"class":144},[138,3444,3445],{"class":199},"\"abc\"",[138,3447,861],{"class":144},[22,3449,3451,3465,3475,3488],{"className":3450},[25],[27,3452,3454,218,3456,234,3458,3460,3461,3464],{"className":3453},[30],[32,3455],{"disabled":34,"type":35},[38,3457,3270],{},[38,3459,2175],{}," — the IIFE leaks its inner variables onto ",[38,3462,3463],{},"cache"," automatically",[27,3466,3468,3283,3470,3472,3473],{"className":3467},[30],[32,3469],{"disabled":34,"type":35},[38,3471,245],{},", then nothing logs — IIFEs cannot reach outer variables like ",[38,3474,3463],{},[27,3476,3478,218,3480,234,3482,963,3484,3487],{"className":3477},[30],[32,3479],{"disabled":34,"type":35},[38,3481,233],{},[38,3483,2175],{},[38,3485,3486],{},"secretSalt"," is hoisted as a function",[27,3489,3491,218,3493,234,3495,3497,3498,3500,3501,3504],{"className":3490},[30],[32,3492],{"disabled":34,"type":35},[38,3494,221],{},[38,3496,2175],{}," — the IIFE runs immediately and creates a private scope, so ",[38,3499,3486],{}," never escapes, while ",[38,3502,3503],{},"cache.hash"," retains a reference to it",[65,3506,3507,3509,3521],{},[68,3508,70],{},[72,3510,3511,581,3513,234,3515,3497,3517,3500,3519,3504],{},[75,3512,77],{},[38,3514,221],{},[38,3516,2175],{},[38,3518,3486],{},[38,3520,3503],{},[72,3522,3523,218,3525,3527,3528,3530,3531,3534,3535,218,3538,3540,3541,3543,3544,3546,3547,3550,3551,3554],{},[75,3524,87],{},[75,3526,2431],{}," an IIFE (immediately invoked function expression) is defined and called in one step specifically to create an isolated, private scope — a pattern that predates block scoping and is still useful for module-style encapsulation. ",[38,3529,3486],{}," lives only inside ",[38,3532,3533],{},"initCache","'s scope, so it's never visible outside, making ",[38,3536,3537],{},"typeof secretSalt",[38,3539,221],{},". The function assigned to ",[38,3542,3503],{}," was created inside that same scope though, so it can still read ",[38,3545,3486],{}," when it's called later, producing ",[38,3548,3549],{},"3 + 6 = 9",". Option A gets the mechanism backwards — the whole point of an IIFE is to ",[94,3552,3553],{},"prevent"," leakage, not cause it.",[3556,3557,3558],"style",{},"html pre.shiki code .ssxIu, html code.shiki .ssxIu{--shiki-default:#24292E;--shiki-github-dark:#E1E4E8}html pre.shiki code .sIsaT, html code.shiki .sIsaT{--shiki-default:#6F42C1;--shiki-github-dark:#B392F0}html pre.shiki code .svdQ7, html code.shiki .svdQ7{--shiki-default:#D73A49;--shiki-github-dark:#F97583}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 .sCrzJ, html code.shiki .sCrzJ{--shiki-default:#E36209;--shiki-github-dark:#FFAB70}html pre.shiki code .snvgF, html code.shiki .snvgF{--shiki-default:#005CC5;--shiki-github-dark:#79B8FF}",{"title":134,"searchDepth":162,"depth":162,"links":3560},[3561,3562,3563,3564,3565,3566,3567,3568,3569,3570,3571,3572,3573,3574,3575,3576,3577,3578,3580,3581],{"id":19,"depth":171,"text":20},{"id":124,"depth":171,"text":125},{"id":303,"depth":171,"text":304},{"id":463,"depth":171,"text":464},{"id":618,"depth":171,"text":619},{"id":763,"depth":171,"text":764},{"id":984,"depth":171,"text":985},{"id":1197,"depth":171,"text":1198},{"id":1404,"depth":171,"text":1405},{"id":1571,"depth":171,"text":1572},{"id":1712,"depth":171,"text":1713},{"id":1872,"depth":171,"text":1873},{"id":2034,"depth":171,"text":2035},{"id":2280,"depth":171,"text":2281},{"id":2444,"depth":171,"text":2445},{"id":2648,"depth":171,"text":2649},{"id":2836,"depth":171,"text":2837},{"id":3027,"depth":171,"text":3579},"Q18. What is request.length in this snippet?",{"id":3177,"depth":171,"text":3178},{"id":3328,"depth":171,"text":3329},"md",{},"\u002Fjs\u002F05-functions-and-scope",{"title":5,"description":134},"js\u002F05-functions-and-scope","6xws2YRG2sebawVwbCa7OP71onkvRltqmwayd9JWW58",1787335397332]