{"id":62,"date":"2026-05-01T10:45:43","date_gmt":"2026-05-01T10:45:43","guid":{"rendered":"https:\/\/gigz.pk\/javascript\/?post_type=lesson&#038;p=62"},"modified":"2026-05-01T10:45:44","modified_gmt":"2026-05-01T10:45:44","slug":"var-let-const","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/javascript\/?lesson=var-let-const","title":{"rendered":"var let const"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Introduction<br>Variables are used to store data in JavaScript. They allow developers to save values like text numbers or objects and reuse them in a program. JavaScript provides three ways to declare variables which are var let and const. Each has different behavior and use cases.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Understanding var<br>The var keyword is the oldest way to declare variables in JavaScript. It has function scope which means it is accessible throughout the function where it is defined. If declared outside a function it becomes globally accessible.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Variables declared with var can be redeclared and updated. This flexibility can sometimes lead to errors in large programs.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example<br>var name = &#8220;Ali&#8221;<br>var name = &#8220;Ahmed&#8221;<br>name = &#8220;Sara&#8221;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Understanding let<br>The let keyword was introduced in modern JavaScript to solve issues with var. It has block scope which means it only works inside the block where it is defined such as inside loops or conditions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Variables declared with let can be updated but cannot be redeclared in the same scope.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example<br>let age = 20<br>age = 25<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Understanding const<br>The const keyword is used to declare variables whose values should not change. It also has block scope like let. Once a value is assigned to a const variable it cannot be reassigned.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However if the value is an object or array its contents can still be modified.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example<br>const country = &#8220;Pakistan&#8221;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Key Differences<br>var is function scoped while let and const are block scoped<br>var allows redeclaration but let and const do not<br>let allows updates but const does not allow reassignment<br>const must be initialized at the time of declaration<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When to Use var let const<br>Use let when the value may change during the program<br>Use const when the value should remain constant<br>Avoid using var in modern JavaScript as let and const are safer<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Conclusion<br>Understanding var let and const is essential for writing clean and error free JavaScript code. Using the correct variable type improves readability and prevents bugs.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/javascript\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">JavaScript Fundamentals (Beginner Level) > Variables and Data Types > var, let, const<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1777632316277\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1777632316034\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"menu_order":0,"template":"","class_list":["post-62","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>var let const - Learn JavaScript with GIGZ.PK<\/title>\n<meta name=\"description\" content=\"Learn JavaScript var let and const with examples understand scope differences and write clean error free code easily\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/javascript\/?lesson=var-let-const\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"var let const - Learn JavaScript with GIGZ.PK\" \/>\n<meta property=\"og:description\" content=\"Learn JavaScript var let and const with examples understand scope differences and write clean error free code easily\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/javascript\/?lesson=var-let-const\" \/>\n<meta property=\"og:site_name\" content=\"Learn JavaScript with GIGZ.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-01T10:45:44+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=var-let-const\",\"url\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=var-let-const\",\"name\":\"var let const - Learn JavaScript with GIGZ.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/#website\"},\"datePublished\":\"2026-05-01T10:45:43+00:00\",\"dateModified\":\"2026-05-01T10:45:44+00:00\",\"description\":\"Learn JavaScript var let and const with examples understand scope differences and write clean error free code easily\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=var-let-const#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=var-let-const\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=var-let-const#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/javascript\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript Fundamentals (Beginner Level) > Variables and Data Types > var, let, const\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/\",\"name\":\"Learn JavaScript with GIGZ.PK\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"var let const - Learn JavaScript with GIGZ.PK","description":"Learn JavaScript var let and const with examples understand scope differences and write clean error free code easily","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gigz.pk\/javascript\/?lesson=var-let-const","og_locale":"en_US","og_type":"article","og_title":"var let const - Learn JavaScript with GIGZ.PK","og_description":"Learn JavaScript var let and const with examples understand scope differences and write clean error free code easily","og_url":"https:\/\/gigz.pk\/javascript\/?lesson=var-let-const","og_site_name":"Learn JavaScript with GIGZ.PK","article_modified_time":"2026-05-01T10:45:44+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/javascript\/?lesson=var-let-const","url":"https:\/\/gigz.pk\/javascript\/?lesson=var-let-const","name":"var let const - Learn JavaScript with GIGZ.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/javascript\/#website"},"datePublished":"2026-05-01T10:45:43+00:00","dateModified":"2026-05-01T10:45:44+00:00","description":"Learn JavaScript var let and const with examples understand scope differences and write clean error free code easily","breadcrumb":{"@id":"https:\/\/gigz.pk\/javascript\/?lesson=var-let-const#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/javascript\/?lesson=var-let-const"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/javascript\/?lesson=var-let-const#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/javascript"},{"@type":"ListItem","position":2,"name":"JavaScript Fundamentals (Beginner Level) > Variables and Data Types > var, let, const"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/javascript\/#website","url":"https:\/\/gigz.pk\/javascript\/","name":"Learn JavaScript with GIGZ.PK","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/javascript\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=\/wp\/v2\/lesson\/62","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=62"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}