{"id":39266,"date":"2025-11-14T11:41:03","date_gmt":"2025-11-14T19:41:03","guid":{"rendered":"https:\/\/cloudinary.com\/blog\/?p=39266"},"modified":"2025-11-14T11:41:03","modified_gmt":"2025-11-14T19:41:03","slug":"what-is-variable-scope-in-javascript","status":"publish","type":"post","link":"https:\/\/cloudinary.com\/blog\/questions\/what-is-variable-scope-in-javascript\/","title":{"rendered":"What Is Variable Scope in JavaScript?"},"content":{"rendered":"\n<p>You have probably seen questions pop up in dev forums like: \u201cwhy does this variable show as undefined\u201d, \u201cwhy do all my setTimeouts log the same value\u201d, or \u201cwhy does a value leak into the global object\u201d? These are all scope questions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Question:<\/h2>\n\n\n\n<p><em>Hi all,<\/em><br><em>I\u2019m new to coding, and I keep running into confusing behavior with variables in JavaScript. Sometimes I can access a variable outside where I declared it, sometimes not. I have also seen weird issues with loops and async code capturing the wrong values.&nbsp;<\/em><\/p>\n\n\n\n<p><em>What is variable scope in JavaScript? How do var, let, and const behave differently with function scope, block scope, and module scope?<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Answer:<\/h2>\n\n\n\n<p>In JavaScript, scope determines where a variable is visible and how long it lives. Understanding scope helps you write predictable code, avoid name collisions, and reason about async behavior. Let\u2019s break it down:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Scope types<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Global scope:<\/strong> Accessible everywhere in your program.<\/li>\n\n\n\n<li><strong>Function scope:<\/strong> Variables declared inside a function are only available in that function.<\/li>\n\n\n\n<li><strong>Block scope:<\/strong> Variables declared with let or const inside a block <code>{ ... }<\/code> are only visible in that block.<\/li>\n\n\n\n<li><strong>Module scope:<\/strong> In ES modules, each file has its own scope. Exports and imports control visibility.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">var vs let vs const<\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-comment\">\/\/ Block scope vs function scope<\/span>\n<span class=\"hljs-keyword\">if<\/span> (<span class=\"hljs-literal\">true<\/span>) {\n\u00a0 <span class=\"hljs-keyword\">var<\/span> a = <span class=\"hljs-number\">1<\/span>;\u00a0 \u00a0 \u00a0 <span class=\"hljs-comment\">\/\/ function-scoped<\/span>\n\u00a0 <span class=\"hljs-keyword\">let<\/span> b = <span class=\"hljs-number\">2<\/span>;\u00a0 \u00a0 \u00a0 <span class=\"hljs-comment\">\/\/ block-scoped<\/span>\n\u00a0 <span class=\"hljs-keyword\">const<\/span> c = <span class=\"hljs-number\">3<\/span>;\u00a0 \u00a0 <span class=\"hljs-comment\">\/\/ block-scoped<\/span>\n}\n<span class=\"hljs-built_in\">console<\/span>.log(a); <span class=\"hljs-comment\">\/\/ 1<\/span>\n<span class=\"hljs-built_in\">console<\/span>.log(b); <span class=\"hljs-comment\">\/\/ ReferenceError<\/span>\n<span class=\"hljs-built_in\">console<\/span>.log(c); <span class=\"hljs-comment\">\/\/ ReferenceError<\/span>\n\n<span class=\"hljs-comment\">\/\/ Mutability<\/span>\n<span class=\"hljs-keyword\">let<\/span> counter = <span class=\"hljs-number\">0<\/span>;\ncounter += <span class=\"hljs-number\">1<\/span>; \u00a0 \u00a0 <span class=\"hljs-comment\">\/\/ ok<\/span>\n\n<span class=\"hljs-keyword\">const<\/span> apiBase = <span class=\"hljs-string\">'\/api'<\/span>;\napiBase = <span class=\"hljs-string\">'\/v2'<\/span>;\u00a0 <span class=\"hljs-comment\">\/\/ TypeError - cannot reassign const<\/span>\n\n<span class=\"hljs-comment\">\/\/ Note: const prevents reassignment, not mutation of objects<\/span>\n<span class=\"hljs-keyword\">const<\/span> config = { <span class=\"hljs-attr\">retries<\/span>: <span class=\"hljs-number\">3<\/span> };\nconfig.retries = <span class=\"hljs-number\">5<\/span>; <span class=\"hljs-comment\">\/\/ ok<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Hoisting and the temporal dead zone<\/h3>\n\n\n\n<p>Declarations are hoisted, but initialization differs:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-built_in\">console<\/span>.log(x); <span class=\"hljs-comment\">\/\/ undefined - var is hoisted with default undefined<\/span>\n<span class=\"hljs-keyword\">var<\/span> x = <span class=\"hljs-number\">10<\/span>;\n\n<span class=\"hljs-built_in\">console<\/span>.log(y); <span class=\"hljs-comment\">\/\/ ReferenceError - y is in temporal dead zone<\/span>\n<span class=\"hljs-keyword\">let<\/span> y = <span class=\"hljs-number\">10<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Prefer <code>let<\/code> and <code>const<\/code> to catch usage before declaration and avoid subtle bugs.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Closures and capturing variables<\/h3>\n\n\n\n<p>Closures let inner functions access variables from outer scopes. The classic gotcha happens with loops.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-comment\">\/\/ Problem: var is function-scoped, so all callbacks see the final i<\/span>\n<span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-keyword\">var<\/span> i = <span class=\"hljs-number\">0<\/span>; i &lt; <span class=\"hljs-number\">3<\/span>; i++) {\n\u00a0 setTimeout(<span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> <span class=\"hljs-built_in\">console<\/span>.log(<span class=\"hljs-string\">'var i:'<\/span>, i), <span class=\"hljs-number\">0<\/span>); <span class=\"hljs-comment\">\/\/ prints 3,3,3<\/span>\n}\n\n<span class=\"hljs-comment\">\/\/ Fix 1: use let for block scoping<\/span>\n<span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-keyword\">let<\/span> j = <span class=\"hljs-number\">0<\/span>; j &lt; <span class=\"hljs-number\">3<\/span>; j++) {\n\u00a0 setTimeout(<span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> <span class=\"hljs-built_in\">console<\/span>.log(<span class=\"hljs-string\">'let j:'<\/span>, j), <span class=\"hljs-number\">0<\/span>); <span class=\"hljs-comment\">\/\/ prints 0,1,2<\/span>\n}\n\n<span class=\"hljs-comment\">\/\/ Fix 2: capture via IIFE<\/span>\n<span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-keyword\">var<\/span> k = <span class=\"hljs-number\">0<\/span>; k &lt; <span class=\"hljs-number\">3<\/span>; k++) {\n\u00a0 (<span class=\"hljs-function\">(<span class=\"hljs-params\">n<\/span>) =&gt;<\/span> setTimeout(<span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> <span class=\"hljs-built_in\">console<\/span>.log(<span class=\"hljs-string\">'IIFE k:'<\/span>, n), <span class=\"hljs-number\">0<\/span>))(k);\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Module scope<\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-comment\">\/\/ config.js<\/span>\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">const<\/span> API_BASE = <span class=\"hljs-string\">'\/api'<\/span>;\n\n<span class=\"hljs-comment\">\/\/ users.js<\/span>\n<span class=\"hljs-keyword\">import<\/span> { API_BASE } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/config.js'<\/span>;\nfetch(<span class=\"hljs-string\">`<span class=\"hljs-subst\">${API_BASE}<\/span>\/users`<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Modules isolate variables by default, so use exports to share and imports to consume. Unlike scripts, top-level <code>this<\/code> is undefined in modules and strict mode is implied.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Common pitfalls and best practices<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Avoid globals. Pass values as parameters or import from modules.<\/li>\n\n\n\n<li>Prefer using <strong>const<\/strong> and upgrade to <strong>let<\/strong> only when reassignment is necessary.<\/li>\n\n\n\n<li>Initialize variables close to where they are used to minimize scope.<\/li>\n\n\n\n<li>In async code, prefer <strong>let<\/strong> in loops or capture values explicitly.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Applying scope in real apps with a media workflow example<\/h3>\n\n\n\n<p>When building front-end or Node services that deliver images and videos, keep configuration and secrets scoped safely. For example, host and optimize visuals while keeping API credentials private and configuration well-contained. See an overview of asset delivery considerations in <a href=\"https:\/\/cloudinary.com\/guides\/web-performance\/understanding-image-hosting-for-websites\">understanding image hosting for websites<\/a>.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-comment\">\/\/ Node: keep credentials in module scope with environment variables<\/span>\n<span class=\"hljs-comment\">\/\/ cloud.js<\/span>\n<span class=\"hljs-keyword\">import<\/span> { v2 <span class=\"hljs-keyword\">as<\/span> cloud } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'cloudinary'<\/span>;\ncloud.config({\n\u00a0 <span class=\"hljs-attr\">cloud_name<\/span>: process.env.CLOUDINARY_CLOUD_NAME,\n\u00a0 <span class=\"hljs-attr\">api_key<\/span>: process.env.CLOUDINARY_API_KEY,\n\u00a0 <span class=\"hljs-attr\">api_secret<\/span>: process.env.CLOUDINARY_API_SECRET\n});\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> cloud;\n\n<span class=\"hljs-comment\">\/\/ uploader.js<\/span>\n<span class=\"hljs-keyword\">import<\/span> cloud <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/cloud.js'<\/span>;\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">uploadImage<\/span>(<span class=\"hljs-params\">path<\/span>) <\/span>{\n\u00a0 <span class=\"hljs-keyword\">return<\/span> cloud.uploader.upload(path, { <span class=\"hljs-attr\">folder<\/span>: <span class=\"hljs-string\">'scoped-demo'<\/span> });\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the browser, never expose secrets. Use unsigned upload presets and keep them in a local constant that is not globally leaked:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">const<\/span> UPLOAD_PRESET = <span class=\"hljs-string\">'unsigned_preset'<\/span>; <span class=\"hljs-comment\">\/\/ safe to expose<\/span>\n<span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">upload<\/span>(<span class=\"hljs-params\">file<\/span>) <\/span>{\n\u00a0 <span class=\"hljs-keyword\">const<\/span> url = <span class=\"hljs-string\">'https:\/\/api.cloudinary.com\/v1_1\/demo\/image\/upload'<\/span>;\n\u00a0 <span class=\"hljs-keyword\">const<\/span> fd = <span class=\"hljs-keyword\">new<\/span> FormData();\n\u00a0 fd.append(<span class=\"hljs-string\">'file'<\/span>, file);\n\u00a0 fd.append(<span class=\"hljs-string\">'upload_preset'<\/span>, UPLOAD_PRESET);\n\u00a0 <span class=\"hljs-keyword\">await<\/span> fetch(url, { <span class=\"hljs-attr\">method<\/span>: <span class=\"hljs-string\">'POST'<\/span>, <span class=\"hljs-attr\">body<\/span>: fd });\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>When generating transformation URLs, build them inside well-scoped helpers and avoid polluting global scope. If you are optimizing delivery, these helpers often encapsulate logic like format switching and size rules for an <a href=\"https:\/\/cloudinary.com\/guides\/web-performance\/what-is-an-optimized-website-and-6-ways-to-optimize-yours\">optimized website<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">TL;DR<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Scope controls visibility and lifetime of variables: global, function, block, module.<\/li>\n\n\n\n<li>Use const by default, let when needed, avoid var.<\/li>\n\n\n\n<li>Hoisting makes var visible as undefined but let and const have a temporal dead zone.<\/li>\n\n\n\n<li>Closures capture variables from outer scopes. Use let in loops to avoid async pitfalls.<\/li>\n\n\n\n<li>In real apps, keep secrets in server scope, avoid global leaks, and encapsulate helpers for media URLs and uploads.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Learn More<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/cloudinary.com\/tools\/png-to-webp\">PNG to WebP Converter<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/cloudinary.com\/tools\/jpg-to-png\">JPG to PNG<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/cloudinary.com\/tools\/webm-to-mp4\">WEBM to MP4<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/cloudinary.com\/background-remover\">Background Remover<\/a><\/li>\n<\/ul>\n\n\n\n<p>Ready to streamline your media workflow while keeping your JavaScript code clean and well scoped? <a href=\"https:\/\/cloudinary.com\/users\/register_free\">Sign up for a free Cloudinary account<\/a> and start optimizing today.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You have probably seen questions pop up in dev forums like: \u201cwhy does this variable show as undefined\u201d, \u201cwhy do all my setTimeouts log the same value\u201d, or \u201cwhy does a value leak into the global object\u201d? These are all scope questions. Question: Hi all,I\u2019m new to coding, and I keep running into confusing behavior [&hellip;]<\/p>\n","protected":false},"author":88,"featured_media":39267,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_cloudinary_featured_overwrite":false,"footnotes":""},"categories":[1],"tags":[423],"class_list":["post-39266","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-questions"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v25.6 (Yoast SEO v26.9) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>What Is Variable Scope in JavaScript?<\/title>\n<meta name=\"description\" content=\"You have probably seen questions pop up in dev forums like: \u201cwhy does this variable show as undefined\u201d, \u201cwhy do all my setTimeouts log the same value\u201d, or\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/cloudinary.com\/blog\/questions\/what-is-variable-scope-in-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"What Is Variable Scope in JavaScript?\" \/>\n<meta property=\"og:description\" content=\"You have probably seen questions pop up in dev forums like: \u201cwhy does this variable show as undefined\u201d, \u201cwhy do all my setTimeouts log the same value\u201d, or\" \/>\n<meta property=\"og:url\" content=\"https:\/\/cloudinary.com\/blog\/questions\/what-is-variable-scope-in-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"Cloudinary Blog\" \/>\n<meta property=\"article:published_time\" content=\"2025-11-14T19:41:03+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1763149247\/QA_javascript_featured_image\/QA_javascript_featured_image.jpg?_i=AA\" \/>\n\t<meta property=\"og:image:width\" content=\"1999\" \/>\n\t<meta property=\"og:image:height\" content=\"1100\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"damjanantevski\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"NewsArticle\",\"@id\":\"https:\/\/cloudinary.com\/blog\/questions\/what-is-variable-scope-in-javascript\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/questions\/what-is-variable-scope-in-javascript\/\"},\"author\":{\"name\":\"damjanantevski\",\"@id\":\"https:\/\/cloudinary.com\/blog\/#\/schema\/person\/43592e43c12520a1e867d456b1e8cf7e\"},\"headline\":\"What Is Variable Scope in JavaScript?\",\"datePublished\":\"2025-11-14T19:41:03+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/questions\/what-is-variable-scope-in-javascript\/\"},\"wordCount\":552,\"publisher\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/questions\/what-is-variable-scope-in-javascript\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1763149247\/QA_javascript_featured_image\/QA_javascript_featured_image.jpg?_i=AA\",\"keywords\":[\"Questions\"],\"inLanguage\":\"en-US\",\"copyrightYear\":\"2025\",\"copyrightHolder\":{\"@id\":\"https:\/\/cloudinary.com\/#organization\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/cloudinary.com\/blog\/questions\/what-is-variable-scope-in-javascript\/\",\"url\":\"https:\/\/cloudinary.com\/blog\/questions\/what-is-variable-scope-in-javascript\/\",\"name\":\"What Is Variable Scope in JavaScript?\",\"isPartOf\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/questions\/what-is-variable-scope-in-javascript\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/questions\/what-is-variable-scope-in-javascript\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1763149247\/QA_javascript_featured_image\/QA_javascript_featured_image.jpg?_i=AA\",\"datePublished\":\"2025-11-14T19:41:03+00:00\",\"description\":\"You have probably seen questions pop up in dev forums like: \u201cwhy does this variable show as undefined\u201d, \u201cwhy do all my setTimeouts log the same value\u201d, or\",\"breadcrumb\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/questions\/what-is-variable-scope-in-javascript\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/cloudinary.com\/blog\/questions\/what-is-variable-scope-in-javascript\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/cloudinary.com\/blog\/questions\/what-is-variable-scope-in-javascript\/#primaryimage\",\"url\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1763149247\/QA_javascript_featured_image\/QA_javascript_featured_image.jpg?_i=AA\",\"contentUrl\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1763149247\/QA_javascript_featured_image\/QA_javascript_featured_image.jpg?_i=AA\",\"width\":1999,\"height\":1100},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/cloudinary.com\/blog\/questions\/what-is-variable-scope-in-javascript\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/cloudinary.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"What Is Variable Scope in JavaScript?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/cloudinary.com\/blog\/#website\",\"url\":\"https:\/\/cloudinary.com\/blog\/\",\"name\":\"Cloudinary Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/cloudinary.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/cloudinary.com\/blog\/#organization\",\"name\":\"Cloudinary Blog\",\"url\":\"https:\/\/cloudinary.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/cloudinary.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649718331\/Web_Assets\/blog\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877.png?_i=AA\",\"contentUrl\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649718331\/Web_Assets\/blog\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877.png?_i=AA\",\"width\":312,\"height\":60,\"caption\":\"Cloudinary Blog\"},\"image\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/cloudinary.com\/blog\/#\/schema\/person\/43592e43c12520a1e867d456b1e8cf7e\",\"name\":\"damjanantevski\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/cloudinary.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/3b40c995531fe4d510212a06c9d4fc666d2cb8efbfebc98a94191701accf4817?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/3b40c995531fe4d510212a06c9d4fc666d2cb8efbfebc98a94191701accf4817?s=96&d=mm&r=g\",\"caption\":\"damjanantevski\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"What Is Variable Scope in JavaScript?","description":"You have probably seen questions pop up in dev forums like: \u201cwhy does this variable show as undefined\u201d, \u201cwhy do all my setTimeouts log the same value\u201d, or","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:\/\/cloudinary.com\/blog\/questions\/what-is-variable-scope-in-javascript\/","og_locale":"en_US","og_type":"article","og_title":"What Is Variable Scope in JavaScript?","og_description":"You have probably seen questions pop up in dev forums like: \u201cwhy does this variable show as undefined\u201d, \u201cwhy do all my setTimeouts log the same value\u201d, or","og_url":"https:\/\/cloudinary.com\/blog\/questions\/what-is-variable-scope-in-javascript\/","og_site_name":"Cloudinary Blog","article_published_time":"2025-11-14T19:41:03+00:00","og_image":[{"width":1999,"height":1100,"url":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1763149247\/QA_javascript_featured_image\/QA_javascript_featured_image.jpg?_i=AA","type":"image\/jpeg"}],"author":"damjanantevski","twitter_card":"summary_large_image","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"NewsArticle","@id":"https:\/\/cloudinary.com\/blog\/questions\/what-is-variable-scope-in-javascript\/#article","isPartOf":{"@id":"https:\/\/cloudinary.com\/blog\/questions\/what-is-variable-scope-in-javascript\/"},"author":{"name":"damjanantevski","@id":"https:\/\/cloudinary.com\/blog\/#\/schema\/person\/43592e43c12520a1e867d456b1e8cf7e"},"headline":"What Is Variable Scope in JavaScript?","datePublished":"2025-11-14T19:41:03+00:00","mainEntityOfPage":{"@id":"https:\/\/cloudinary.com\/blog\/questions\/what-is-variable-scope-in-javascript\/"},"wordCount":552,"publisher":{"@id":"https:\/\/cloudinary.com\/blog\/#organization"},"image":{"@id":"https:\/\/cloudinary.com\/blog\/questions\/what-is-variable-scope-in-javascript\/#primaryimage"},"thumbnailUrl":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1763149247\/QA_javascript_featured_image\/QA_javascript_featured_image.jpg?_i=AA","keywords":["Questions"],"inLanguage":"en-US","copyrightYear":"2025","copyrightHolder":{"@id":"https:\/\/cloudinary.com\/#organization"}},{"@type":"WebPage","@id":"https:\/\/cloudinary.com\/blog\/questions\/what-is-variable-scope-in-javascript\/","url":"https:\/\/cloudinary.com\/blog\/questions\/what-is-variable-scope-in-javascript\/","name":"What Is Variable Scope in JavaScript?","isPartOf":{"@id":"https:\/\/cloudinary.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/cloudinary.com\/blog\/questions\/what-is-variable-scope-in-javascript\/#primaryimage"},"image":{"@id":"https:\/\/cloudinary.com\/blog\/questions\/what-is-variable-scope-in-javascript\/#primaryimage"},"thumbnailUrl":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1763149247\/QA_javascript_featured_image\/QA_javascript_featured_image.jpg?_i=AA","datePublished":"2025-11-14T19:41:03+00:00","description":"You have probably seen questions pop up in dev forums like: \u201cwhy does this variable show as undefined\u201d, \u201cwhy do all my setTimeouts log the same value\u201d, or","breadcrumb":{"@id":"https:\/\/cloudinary.com\/blog\/questions\/what-is-variable-scope-in-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/cloudinary.com\/blog\/questions\/what-is-variable-scope-in-javascript\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cloudinary.com\/blog\/questions\/what-is-variable-scope-in-javascript\/#primaryimage","url":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1763149247\/QA_javascript_featured_image\/QA_javascript_featured_image.jpg?_i=AA","contentUrl":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1763149247\/QA_javascript_featured_image\/QA_javascript_featured_image.jpg?_i=AA","width":1999,"height":1100},{"@type":"BreadcrumbList","@id":"https:\/\/cloudinary.com\/blog\/questions\/what-is-variable-scope-in-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/cloudinary.com\/blog\/"},{"@type":"ListItem","position":2,"name":"What Is Variable Scope in JavaScript?"}]},{"@type":"WebSite","@id":"https:\/\/cloudinary.com\/blog\/#website","url":"https:\/\/cloudinary.com\/blog\/","name":"Cloudinary Blog","description":"","publisher":{"@id":"https:\/\/cloudinary.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/cloudinary.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/cloudinary.com\/blog\/#organization","name":"Cloudinary Blog","url":"https:\/\/cloudinary.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cloudinary.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649718331\/Web_Assets\/blog\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877.png?_i=AA","contentUrl":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649718331\/Web_Assets\/blog\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877.png?_i=AA","width":312,"height":60,"caption":"Cloudinary Blog"},"image":{"@id":"https:\/\/cloudinary.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/cloudinary.com\/blog\/#\/schema\/person\/43592e43c12520a1e867d456b1e8cf7e","name":"damjanantevski","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cloudinary.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/3b40c995531fe4d510212a06c9d4fc666d2cb8efbfebc98a94191701accf4817?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3b40c995531fe4d510212a06c9d4fc666d2cb8efbfebc98a94191701accf4817?s=96&d=mm&r=g","caption":"damjanantevski"}}]}},"jetpack_featured_media_url":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1763149247\/QA_javascript_featured_image\/QA_javascript_featured_image.jpg?_i=AA","_links":{"self":[{"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/posts\/39266","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/users\/88"}],"replies":[{"embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/comments?post=39266"}],"version-history":[{"count":1,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/posts\/39266\/revisions"}],"predecessor-version":[{"id":39268,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/posts\/39266\/revisions\/39268"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/media\/39267"}],"wp:attachment":[{"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/media?parent=39266"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/categories?post=39266"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/tags?post=39266"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}