[{"data":1,"prerenderedAt":846},["ShallowReactive",2],{"navigation_docs":3,"-programming-javascript-variables-and-declarations":30,"-programming-javascript-variables-and-declarations-surround":841},[4,9],{"title":5,"path":6,"stem":7,"icon":8},"Welcome","\u002Fhome","1.home","i-lucide-rocket",{"title":10,"icon":11,"path":12,"stem":13,"children":14,"page":29},"Programming","i-lucide-code","\u002Fprogramming","2.programming",[15],{"title":16,"icon":17,"path":18,"stem":19,"children":20,"page":29},"JavaScript","i-lucide-file-braces-corner","\u002Fprogramming\u002Fjavascript","2.programming\u002F1.javascript",[21,25],{"title":22,"path":23,"stem":24},"1. Variables & Declarations","\u002Fprogramming\u002Fjavascript\u002Fvariables-and-declarations","2.programming\u002F1.javascript\u002F1.variables-and-declarations",{"title":26,"path":27,"stem":28},"2. Data Types + Type System","\u002Fprogramming\u002Fjavascript\u002Fdata-types-type-system","2.programming\u002F1.javascript\u002F2.data-types-type-system",false,{"id":31,"title":32,"body":33,"description":832,"extension":833,"links":834,"meta":835,"navigation":836,"path":23,"seo":837,"stem":24,"__hash__":840},"docs\u002F2.programming\u002F1.javascript\u002F1.variables-and-declarations.md","Chapter 1: Variables & Declarations",{"type":34,"value":35,"toc":819},"minimark",[36,41,75,79,82,116,166,168,196,247,249,274,308,315,388,392,433,548,552,562,567,586,620,653,657,689,693,710,714,728,758,763,795,815],[37,38,40],"h2",{"id":39},"what-are-variables","What are Variables?",[42,43,44,54,57,60],"ul",{},[45,46,47,48,53],"li",{},"Variables are ",[49,50,52],"span",{"style":51},"color: #eab308;","containers that hold data",".",[45,55,56],{},"They help us store, reuse, and update information in JavaScript — from simple values like numbers to complex data like arrays and objects.",[45,58,59],{},"Think of a variable as a box with a name on it. You can put something inside it (a value), and later check or change what's inside.",[45,61,62,63,67,68,71,72,53],{},"In JavaScript, you create these boxes using keywords: ",[64,65,66],"code",{},"var",", ",[64,69,70],{},"let",", or ",[64,73,74],{},"const",[37,76,78],{"id":77},"var-let-and-const","var, let, and const",[80,81,66],"h3",{"id":66},[42,83,84,89,96,106],{},[45,85,86],{},[49,87,88],{"style":51},"Old and Risky",[45,90,91,92,95],{},"Scoped to ",[49,93,94],{"style":51},"functions",", not blocks",[45,97,98,99,102,103],{},"Can be ",[49,100,101],{"style":51},"redeclared"," and ",[49,104,105],{"style":51},"reassigned",[45,107,108,111,112,115],{},[49,109,110],{"style":51},"Hoisted"," to the top with ",[64,113,114],{},"undefined"," value",[117,118,123],"pre",{"className":119,"code":120,"language":121,"meta":122,"style":122},"language-js shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","var score = 10;\nvar score = 20; \u002F\u002F OK\n","js","",[64,124,125,147],{"__ignoreMap":122},[49,126,129,132,136,140,144],{"class":127,"line":128},"line",1,[49,130,66],{"class":131},"spNyl",[49,133,135],{"class":134},"sTEyZ"," score ",[49,137,139],{"class":138},"sMK4o","=",[49,141,143],{"class":142},"sbssI"," 10",[49,145,146],{"class":138},";\n",[49,148,150,152,154,156,159,162],{"class":127,"line":149},2,[49,151,66],{"class":131},[49,153,135],{"class":134},[49,155,139],{"class":138},[49,157,158],{"class":142}," 20",[49,160,161],{"class":138},";",[49,163,165],{"class":164},"sHwdD"," \u002F\u002F OK\n",[80,167,70],{"id":70},[42,169,170,175,185,190],{},[45,171,172],{},[49,173,174],{"style":51},"Modern & Safe",[45,176,91,177,180,181,184],{},[49,178,179],{"style":51},"blocks"," (",[64,182,183],{},"{}",")",[45,186,98,187,189],{},[49,188,105],{"style":51}," but not redeclared",[45,191,192,193],{},"Hoisted, but stays in the ",[49,194,195],{"style":51},"Temporal Dead Zone (TDZ)",[117,197,199],{"className":119,"code":198,"language":121,"meta":122,"style":122},"let age = 25;\nage = 30; \u002F\u002F ✅\nlet age = 40; \u002F\u002F ❌ Error (same block)\n",[64,200,201,215,230],{"__ignoreMap":122},[49,202,203,205,208,210,213],{"class":127,"line":128},[49,204,70],{"class":131},[49,206,207],{"class":134}," age ",[49,209,139],{"class":138},[49,211,212],{"class":142}," 25",[49,214,146],{"class":138},[49,216,217,220,222,225,227],{"class":127,"line":149},[49,218,219],{"class":134},"age ",[49,221,139],{"class":138},[49,223,224],{"class":142}," 30",[49,226,161],{"class":138},[49,228,229],{"class":164}," \u002F\u002F ✅\n",[49,231,233,235,237,239,242,244],{"class":127,"line":232},3,[49,234,70],{"class":131},[49,236,207],{"class":134},[49,238,139],{"class":138},[49,240,241],{"class":142}," 40",[49,243,161],{"class":138},[49,245,246],{"class":164}," \u002F\u002F ❌ Error (same block)\n",[80,248,74],{"id":74},[42,250,251,256,260,268,271],{},[45,252,253],{},[49,254,255],{"style":51},"Constant values",[45,257,91,258],{},[49,259,179],{"style":51},[45,261,262,265,266],{},[49,263,264],{"style":51},"Cannot be reassigned"," or ",[49,267,101],{"style":51},[45,269,270],{},"Value must be assigned at declaration",[45,272,273],{},"TDZ applies here too",[117,275,277],{"className":119,"code":276,"language":121,"meta":122,"style":122},"const PI = 3.14;\nPI = 3.14159; \u002F\u002F ❌ Error\n",[64,278,279,293],{"__ignoreMap":122},[49,280,281,283,286,288,291],{"class":127,"line":128},[49,282,74],{"class":131},[49,284,285],{"class":134}," PI ",[49,287,139],{"class":138},[49,289,290],{"class":142}," 3.14",[49,292,146],{"class":138},[49,294,295,298,300,303,305],{"class":127,"line":149},[49,296,297],{"class":134},"PI ",[49,299,139],{"class":138},[49,301,302],{"class":142}," 3.14159",[49,304,161],{"class":138},[49,306,307],{"class":164}," \u002F\u002F ❌ Error\n",[309,310,311,312,314],"p",{},"But: If ",[64,313,74],{}," holds an object\u002Farray, you can still change its contents:",[117,316,318],{"className":119,"code":317,"language":121,"meta":122,"style":122},"const student = { name: \"Shoaib\" };\nstudent.name = \"Harsh\"; \u002F\u002F ✅ OK\nstudent = {}; \u002F\u002F ❌ Error\n",[64,319,320,352,376],{"__ignoreMap":122},[49,321,322,324,327,329,332,336,339,342,346,349],{"class":127,"line":128},[49,323,74],{"class":131},[49,325,326],{"class":134}," student ",[49,328,139],{"class":138},[49,330,331],{"class":138}," {",[49,333,335],{"class":334},"swJcz"," name",[49,337,338],{"class":138},":",[49,340,341],{"class":138}," \"",[49,343,345],{"class":344},"sfazB","Shoaib",[49,347,348],{"class":138},"\"",[49,350,351],{"class":138}," };\n",[49,353,354,357,359,362,364,366,369,371,373],{"class":127,"line":149},[49,355,356],{"class":134},"student",[49,358,53],{"class":138},[49,360,361],{"class":134},"name ",[49,363,139],{"class":138},[49,365,341],{"class":138},[49,367,368],{"class":344},"Harsh",[49,370,348],{"class":138},[49,372,161],{"class":138},[49,374,375],{"class":164}," \u002F\u002F ✅ OK\n",[49,377,378,381,383,386],{"class":127,"line":232},[49,379,380],{"class":134},"student ",[49,382,139],{"class":138},[49,384,385],{"class":138}," {};",[49,387,307],{"class":164},[37,389,391],{"id":390},"scope-in-real-life","Scope in Real Life",[42,393,394,408,414,424],{},[45,395,396,400,401,403,404,407],{},[397,398,399],"strong",{},"Block Scope"," → Code inside ",[64,402,183],{}," like in loops, ",[64,405,406],{},"if",", etc.",[45,409,410,413],{},[397,411,412],{},"Function Scope"," → Code inside a function",[45,415,416,102,418,420,421,53],{},[64,417,70],{},[64,419,74],{}," follow ",[49,422,423],{"style":51},"block scope",[45,425,426,428,429,432],{},[64,427,66],{}," ",[49,430,431],{"style":51},"ignores block scope"," — which leads to bugs.",[117,434,436],{"className":119,"code":435,"language":121,"meta":122,"style":122},"{\n  var x = 5;\n  let y = 10;\n  const z = 15;\n}\nconsole.log(x); \u002F\u002F ✅ 5\nconsole.log(y); \u002F\u002F ❌ ReferenceError\nconsole.log(z); \u002F\u002F ❌ ReferenceError\n",[64,437,438,443,459,473,489,495,515,532],{"__ignoreMap":122},[49,439,440],{"class":127,"line":128},[49,441,442],{"class":138},"{\n",[49,444,445,448,451,454,457],{"class":127,"line":149},[49,446,447],{"class":131},"  var",[49,449,450],{"class":134}," x",[49,452,453],{"class":138}," =",[49,455,456],{"class":142}," 5",[49,458,146],{"class":138},[49,460,461,464,467,469,471],{"class":127,"line":232},[49,462,463],{"class":131},"  let",[49,465,466],{"class":134}," y",[49,468,453],{"class":138},[49,470,143],{"class":142},[49,472,146],{"class":138},[49,474,476,479,482,484,487],{"class":127,"line":475},4,[49,477,478],{"class":131},"  const",[49,480,481],{"class":134}," z",[49,483,453],{"class":138},[49,485,486],{"class":142}," 15",[49,488,146],{"class":138},[49,490,492],{"class":127,"line":491},5,[49,493,494],{"class":138},"}\n",[49,496,498,501,503,507,510,512],{"class":127,"line":497},6,[49,499,500],{"class":134},"console",[49,502,53],{"class":138},[49,504,506],{"class":505},"s2Zo4","log",[49,508,509],{"class":134},"(x)",[49,511,161],{"class":138},[49,513,514],{"class":164}," \u002F\u002F ✅ 5\n",[49,516,518,520,522,524,527,529],{"class":127,"line":517},7,[49,519,500],{"class":134},[49,521,53],{"class":138},[49,523,506],{"class":505},[49,525,526],{"class":134},"(y)",[49,528,161],{"class":138},[49,530,531],{"class":164}," \u002F\u002F ❌ ReferenceError\n",[49,533,535,537,539,541,544,546],{"class":127,"line":534},8,[49,536,500],{"class":134},[49,538,53],{"class":138},[49,540,506],{"class":505},[49,542,543],{"class":134},"(z)",[49,545,161],{"class":138},[49,547,531],{"class":164},[37,549,551],{"id":550},"hoisting","Hoisting",[42,553,554,557],{},[45,555,556],{},"JavaScript prepares memory before running code.",[45,558,559,560,53],{},"It moves all declarations to the top — this is called ",[49,561,550],{"style":51},[309,563,564],{},[397,565,566],{},"But:",[42,568,569,576],{},[45,570,571,573,574,53],{},[64,572,66],{}," is hoisted and set to ",[64,575,114],{},[45,577,578,102,580,582,583,53],{},[64,579,70],{},[64,581,74],{}," are hoisted but not initialized — so accessing them early gives ",[49,584,585],{"style":51},"ReferenceError",[117,587,589],{"className":119,"code":588,"language":121,"meta":122,"style":122},"console.log(a); \u002F\u002F undefined\nvar a = 10;\n",[64,590,591,607],{"__ignoreMap":122},[49,592,593,595,597,599,602,604],{"class":127,"line":128},[49,594,500],{"class":134},[49,596,53],{"class":138},[49,598,506],{"class":505},[49,600,601],{"class":134},"(a)",[49,603,161],{"class":138},[49,605,606],{"class":164}," \u002F\u002F undefined\n",[49,608,609,611,614,616,618],{"class":127,"line":149},[49,610,66],{"class":131},[49,612,613],{"class":134}," a ",[49,615,139],{"class":138},[49,617,143],{"class":142},[49,619,146],{"class":138},[117,621,623],{"className":119,"code":622,"language":121,"meta":122,"style":122},"console.log(b); \u002F\u002F ❌ ReferenceError\nlet b = 20;\n",[64,624,625,640],{"__ignoreMap":122},[49,626,627,629,631,633,636,638],{"class":127,"line":128},[49,628,500],{"class":134},[49,630,53],{"class":138},[49,632,506],{"class":505},[49,634,635],{"class":134},"(b)",[49,637,161],{"class":138},[49,639,531],{"class":164},[49,641,642,644,647,649,651],{"class":127,"line":149},[49,643,70],{"class":131},[49,645,646],{"class":134}," b ",[49,648,139],{"class":138},[49,650,158],{"class":142},[49,652,146],{"class":138},[37,654,656],{"id":655},"common-confusions","Common Confusions",[42,658,659,667,676],{},[45,660,661,663,664,53],{},[64,662,74],{}," doesn't make things fully constant. ",[49,665,666],{"style":51},"It protects the variable, not the value",[45,668,669,671,672,102,674,53],{},[64,670,66],{}," is outdated — it's better to use ",[64,673,70],{},[64,675,74],{},[45,677,678,102,680,682,683,685,686,53],{},[64,679,70],{},[64,681,74],{}," behave similarly, but ",[64,684,74],{}," gives more safety — ",[49,687,688],{"style":51},"use it when you're not planning to reassign",[37,690,692],{"id":691},"mindset-rule","Mindset Rule",[42,694,695,704],{},[45,696,697,698,700,701,703],{},"Use ",[64,699,74],{}," by default. Use ",[64,702,70],{}," only when you plan to change the value.",[45,705,706,707,709],{},"Avoid ",[64,708,66],{}," — it belongs to the past.",[37,711,713],{"id":712},"practice-zone","Practice Zone",[715,716,717,725],"ol",{},[45,718,719,720,722,723,53],{},"Declare your name and city using ",[64,721,74],{},", and your age using ",[64,724,70],{},[45,726,727],{},"Try this and observe the result:",[117,729,731],{"className":119,"code":730,"language":121,"meta":122,"style":122},"let x = 5;\nlet x = 10;\n",[64,732,733,746],{"__ignoreMap":122},[49,734,735,737,740,742,744],{"class":127,"line":128},[49,736,70],{"class":131},[49,738,739],{"class":134}," x ",[49,741,139],{"class":138},[49,743,456],{"class":142},[49,745,146],{"class":138},[49,747,748,750,752,754,756],{"class":127,"line":149},[49,749,70],{"class":131},[49,751,739],{"class":134},[49,753,139],{"class":138},[49,755,143],{"class":142},[49,757,146],{"class":138},[715,759,760],{"start":232},[45,761,762],{},"Guess the output:",[117,764,766],{"className":119,"code":765,"language":121,"meta":122,"style":122},"console.log(count);\nvar count = 42;\n",[64,767,768,781],{"__ignoreMap":122},[49,769,770,772,774,776,779],{"class":127,"line":128},[49,771,500],{"class":134},[49,773,53],{"class":138},[49,775,506],{"class":505},[49,777,778],{"class":134},"(count)",[49,780,146],{"class":138},[49,782,783,785,788,790,793],{"class":127,"line":149},[49,784,66],{"class":131},[49,786,787],{"class":134}," count ",[49,789,139],{"class":138},[49,791,792],{"class":142}," 42",[49,794,146],{"class":138},[715,796,797,803,809],{"start":475},[45,798,799,800,802],{},"Create a ",[64,801,74],{}," object and add a new key to it — does it work?",[45,804,805,806,808],{},"Try accessing a ",[64,807,70],{}," variable before declaring it — what error do you see?",[45,810,811,812,814],{},"Change a ",[64,813,74],{}," array by pushing a value. Will it throw an error?",[816,817,818],"style",{},"html pre.shiki code .spNyl, html code.shiki .spNyl{--shiki-light:#9C3EDA;--shiki-default:#C792EA;--shiki-dark:#C792EA}html pre.shiki code .sTEyZ, html code.shiki .sTEyZ{--shiki-light:#90A4AE;--shiki-default:#EEFFFF;--shiki-dark:#BABED8}html pre.shiki code .sMK4o, html code.shiki .sMK4o{--shiki-light:#39ADB5;--shiki-default:#89DDFF;--shiki-dark:#89DDFF}html pre.shiki code .sbssI, html code.shiki .sbssI{--shiki-light:#F76D47;--shiki-default:#F78C6C;--shiki-dark:#F78C6C}html pre.shiki code .sHwdD, html code.shiki .sHwdD{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#546E7A;--shiki-default-font-style:italic;--shiki-dark:#676E95;--shiki-dark-font-style:italic}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}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 .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .swJcz, html code.shiki .swJcz{--shiki-light:#E53935;--shiki-default:#F07178;--shiki-dark:#F07178}html pre.shiki code .sfazB, html code.shiki .sfazB{--shiki-light:#91B859;--shiki-default:#C3E88D;--shiki-dark:#C3E88D}html pre.shiki code .s2Zo4, html code.shiki .s2Zo4{--shiki-light:#6182B8;--shiki-default:#82AAFF;--shiki-dark:#82AAFF}",{"title":122,"searchDepth":149,"depth":149,"links":820},[821,822,827,828,829,830,831],{"id":39,"depth":149,"text":40},{"id":77,"depth":149,"text":78,"children":823},[824,825,826],{"id":66,"depth":232,"text":66},{"id":70,"depth":232,"text":70},{"id":74,"depth":232,"text":74},{"id":390,"depth":149,"text":391},{"id":550,"depth":149,"text":551},{"id":655,"depth":149,"text":656},{"id":691,"depth":149,"text":692},{"id":712,"depth":149,"text":713},"Understand variables in JavaScript, including var, let, const, block scope, function scope, and hoisting.","md",null,{},{"title":22},{"title":838,"description":839},"Chapter 1: Variables & Declarations | JavaScript","Learn JavaScript variables, var vs let vs const, scope, and hoisting with real-world examples and practice questions.","I_sNmk_5OIBLyqmO1ZihaGUcMsP0jZvGbY1tMGVt-wA",[842,844],{"title":5,"path":6,"stem":7,"description":843,"icon":8,"children":-1},"Skdev Online Academy is an academic & practical platform empowering learners to master programming and cybersecurity — Learn Beyond Limits.",{"title":26,"path":27,"stem":28,"description":845,"children":-1},"Learn about JavaScript data types, primitive vs reference types, typeof operator, coercion, truthy\u002Ffalsy values, and type system concepts.",1786773830810]