JavaScript Variable Declarations: Var vs. Let vs. Const
JavaScript has three main ways to declare variables: var, const and let. Each has its own purpose and rules. In this article, we will explore what they are and when to use each of them. By the end of this guide, you will have a comprehensive understanding of these concepts, enabling you to improve the clarity and efficiency of your code.
Let's start by looking at the table below and dive on each topic:
Scope
Scope can be defined as the visibility and accessibility of variables, functions and objects.
var name = "Marco"; // Global scope
function example(){
var age = 25; // Function scope
if(age === 25) {
var message = "Marco is 25 years old";
// message is accessible outside the if block, inside the function
// message is not accessible outside the function
}
console.log(message); // Output: "Marco is 25 years old";
}
example();
console.log(name); // Output: Marco
console.log(window.name); // Output: Marco
console.log(age); // Output: ReferenceError: age is not defined
console.log(message); // Output: ReferenceError: message is not defined
let name = "Marco";
function example(){
const age = 25;
if(age === 25){
let message = "Marco is 25 years old";
// message is only accessible inside this if block
}
console.log(message); // ReferenceError: message is not defined
}
example();
console.log(name); // Output: Marco
console.log(window.name); // Output: undefined
console.log(age); // ReferenceError: age is not defined
console.log(message); // ReferenceError: message is not defined
Re-declarations
Re-declaration is when you declare a variable that has already been declared in the same scope. This can lead to errors and unexpected behavior.
var name = "Marco";
console.log(name); // Output: Marco
var name = "Filipe"; // Here it is the re-declaration
console.log(name); // Output: Filipe
let name = "Marco";
console.log(name); // Output: Marco
let name = "Filipe"; // SyntaxError: "name" has already been declared
It is considered a good practice to avoid re-declarations. This means that "var" variables should be avoided, prioritizing "let" and "const".
Re-assignment
Re-assignment means assigning a new value to an existing variable.
var name = "Marco";
console.log(name); // Output: Marco
name = "Filipe"; // Re-assignment
console.log(name); // Output: Filipe
let name = "Marco";
console.log(name); // Output: Marco
name = "Filipe"; // Re-assignment
console.log(name); // Output: Filipe
const name = "Marco";
console.log(name); // Output: Marco
name = "Filipe"; // TypeError: Assignment to constant variable
const colors = ["Red", "Yellow"];
colors.push("Blue"); // This is allowed
console.log(colors); // Output: ["Red", "Yellow", "Blue"]
colors = ["Green", "Black"]; // TypeError: Assignment to constant
领英推荐
Hoisting
Hoisting can be defined as "moving" variables and function declarations to the top of their scope, allowing to use them before they're declared.
Let's focus on "var", "const" and "let" hoisting behaviors:
console.log(name); // Output: undefined
var name = "Marco";
console.log(name); // ReferenceError: cannot access name
const name = "Marco";
console.log(age); // ReferenceError: cannot access age
let age = 25;
Resume
Scope
Re-declaration
Re-assignment
Hoisting
Best Practices
Conclusion
Mastering "var", "const" and "let" declarations in JavaScript is fundamental for writing bug-free and effective code.
By understanding these distinctions, developers can improve code readability, mantainability and avoid common errors and bugs in their JavaScript projects.
Hashtags: #javascript #webdevelopment #var #let #const
Connect with me on X at @CodeWithMarco - https://twitter.com/CodeWithMarco