{
"$type": "site.standard.document",
"bskyPostRef": {
"cid": "bafyreib2h6nmsijk3xo5euvkxuayo2rbsga3dnoycegghjgzths5pfgu5a",
"uri": "at://did:plc:25rdn5elo5izoxrmtis34zuk/app.bsky.feed.post/3mpny7l5wwxv2"
},
"coverImage": {
"$type": "blob",
"ref": {
"$link": "bafkreidspib363dapmdga2ze6atlge3ei6f7tdv5lt7sebmgf7tghmbe6a"
},
"mimeType": "image/webp",
"size": 49338
},
"path": "/adhi_sankar_45ccfb9350749/var-let-and-const-in-javascript-5757",
"publishedAt": "2026-07-02T11:37:25.000Z",
"site": "https://dev.to",
"tags": [
"beginners",
"javascript",
"tutorial",
"webdev"
],
"textContent": "## What is a Variable?\n\nA variable is a named container used to store data in a program. The stored value can be a number, string, object, array, or any other data type.\n\nExample:\n\n\n\n let name = \"John\";\n let age = 25;\n\n\n# 1. `var`\n\n`var` is the oldest way to declare variables in JavaScript. It was used before the introduction of ES6 (ECMAScript 2015).\n\n### Syntax\n\n\n var city = \"Chennai\";\n\n\n### Characteristics\n\n * Function-scoped\n * Can be redeclared\n * Can be reassigned\n * Hoisted and initialized with `undefined`\n\n\n\n### Example\n\n\n var language = \"JavaScript\";\n console.log(language);\n\n language = \"Python\";\n console.log(language);\n\n var language = \"Java\";\n console.log(language);\n\n\n**Output:**\n\n\n\n JavaScript\n Python\n Java\n\n\n### Scope Example\n\n\n if (true) {\n var message = \"Hello\";\n }\n\n console.log(message);\n\n\n**Output:**\n\n\n\n Hello\n\n\nAlthough `message` is declared inside the `if` block, it is still accessible outside because `var` is function-scoped, not block-scoped.\n\n# 2. `let`\n\n`let` was introduced in ES6 and is now the preferred way to declare variables whose values may change.\n\n### Syntax\n\n\n let score = 90;\n\n\n### Characteristics\n\n * Block-scoped\n * Cannot be redeclared in the same scope\n * Can be reassigned\n * Hoisted but not initialized (Temporal Dead Zone)\n\n\n\n### Example\n\n\n let marks = 80;\n\n marks = 95;\n\n console.log(marks);\n\n\n**Output:**\n\n\n\n 95\n\n\n### Block Scope Example\n\n\n if (true) {\n let number = 10;\n console.log(number);\n }\n\n console.log(number);\n\n\n**Output:**\n\n\n\n 10\n ReferenceError: number is not defined\n\n\nThe variable exists only inside the block where it is declared.\n\n# 3. `const`\n\n`const` is also introduced in ES6 and is used for values that should not be reassigned.\n\n### Syntax\n\n\n const PI = 3.14159;\n\n\n### Characteristics\n\n * Block-scoped\n * Cannot be redeclared\n * Cannot be reassigned\n * Must be initialized during declaration\n\n\n\n### Example\n\n\n const country = \"India\";\n\n console.log(country);\n\n\nAttempting to reassign it:\n\n\n\n country = \"USA\";\n\n\n**Output:**\n\n\n\n TypeError: Assignment to constant variable.\n\n\n### Objects with `const`\n\nA `const` object cannot be reassigned, but its properties can be modified.\n\n\n\n const student = {\n name: \"Alex\",\n age: 20\n };\n\n student.age = 21;\n\n console.log(student);\n\n\n**Output:**\n\n\n\n {\n name: \"Alex\",\n age: 21\n }\n\n\n# Difference Between `var`, `let`, and `const`\n\nFeature | `var` | `let` | `const`\n---|---|---|---\nScope | Function | Block | Block\nRedeclaration | Yes | No | No\nReassignment | Yes | Yes | No\nHoisted | Yes | Yes | Yes\nInitialized During Hoisting | Yes (`undefined`) | No | No\nMust Initialize | No | No | Yes\n\n# Hoisting Example\n\n\n console.log(a);\n var a = 5;\n\n\n**Output:**\n\n\n\n undefined\n\n\n\n console.log(b);\n let b = 5;\n\n\n**Output:**\n\n\n\n ReferenceError\n\n\n\n console.log(c);\n const c = 5;\n\n\n**Output:**\n\n\n\n ReferenceError\n\n\n# When Should You Use Each?\n\n### Use `const` when:\n\n * The variable value should not change.\n * Declaring constants like API URLs or configuration values.\n\n\n\nExample:\n\n\n\n const company = \"OpenAI\";\n\n\n### Use `let` when:\n\n * The value needs to change later.\n\n\n\nExample:\n\n\n\n let count = 0;\n\n count++;\n\n console.log(count);\n\n\n### Avoid `var` in modern JavaScript because:\n\n * It ignores block scope.\n * It allows accidental redeclaration.\n * It can introduce bugs in large applications.\n\n\n\n# Best Practices\n\n * Use **`const` by default**.\n * Use **`let` only when the value needs to change**.\n * Avoid using **`var`** in modern JavaScript development.\n * Give variables meaningful names.\n * Keep variable scope as small as possible.\n\n\n\n# Conclusion\n\nUnderstanding the differences between `var`, `let`, and `const` is essential for writing reliable JavaScript code. While `var` was widely used in older JavaScript versions, modern development favors `let` and `const` because they provide block scope and help prevent common programming mistakes.\n\nAs a general rule:\n\n * Use **`const`** for values that should remain unchanged.\n * Use **`let`** for variables whose values will change.\n * Avoid **`var`** unless you're maintaining legacy JavaScript code.\n\n",
"title": "`var`, `let`, and `const` in JavaScript"
}