Understanding Variables in JavaScript: What They Are and How to Use Them


JavaScript Logo

As a software developer, variables are one of the first things we learn when we start programming. However, although they are a basic concept, it is essential to understand how they work in depth to write clear, efficient, and error-free code. In this post, I will explain what variables are in JavaScript, how they work, and how you can use them effectively in your projects.

What is a Variable?

A variable in programming is a container that stores data. Think of it as a labeled box where you can store and retrieve a value when you need it. In JavaScript, a variable can store different types of data, such as numbers, strings, objects, functions, among others.

Declaring Variables in JavaScript

In JavaScript, there are three keywords used to declare variables: var, let, and const. Below, I’ll explain the differences and how to use them::

1. var: The Traditional Way

Historically, var was the only way to declare variables in JavaScript. However, it has some quirks that can lead to errors, especially in larger applications.

var name = "Peter";

One of the characteristics of var is that it has function scope, which means it is accessible throughout the body of the function in which it is declared. Additionally, var allows redeclaration of variables within the same context, which can lead to confusion.

Note: We will delve into what "scope" or "context" means in programming in more detail in another article.

2. let: The Modern Alternative

Introduced in ES6, let is the modern way to declare variables. The main advantage of let is that it has block scope, meaning it is only accessible within the block of code in which it is declared (such as a loop or a condition).

let age = 25;

Another important feature of let is that it does not allow the redeclaration of the same variable within the same block, which helps avoid common errors.

3. const: Constant Variables

Also introduced in ES6, const is used to declare variables whose value should not change over time. Once you assign a value to a const variable, you cannot reassign a new value.

const pi = 3.14159;

It's important to note that const does not make the value immutable if it's an object or an array; what remains constant is the reference to the value, not the value itself.

Note: We will delve into this further when we discuss reference vs. value and inmutability.

Assigning and Using Variables

Once you have declared a variable, you can assign it a value. In JavaScript, this is done with the assignment operator =.

let message = "Hello, world!";

You can change the value of a variable declared with let simply by assigning it a new value.

message = "Hello, JavaScript!";

Nota: Notice that we are not redefining the same variable here (we are not using the reserved word let), which would produce an error in JavaScript. Instead, we are overwriting the value of the same variable we defined earlier

However, as I mentioned before, you cannot do this with a variable declared with const. The following example would produce an error since the value of a constant cannot be changed.

const message = "Hello, world!";
 
message = "Hello, JavaScript!";

Best Practices for Using Variables

Here are some best practices to help you write cleaner, more maintainable code:

  • Use descriptive names: The name of a variable should describe its purpose. For example, userAge is clearer than age if you are storing the age of a user.

  • Keep the scope as narrow as possible: Use let and const to declare variables in the most restricted scope you can. This avoids errors and makes the code easier to follow.

  • Initialize variables: Whenever possible, initialize your variables when you declare them. This prevents errors with undefined values. Instead of this:

    let message;
     
    // At this point, the value of the message is "undefined" until we assign a value to it.
     
    message = "Hello, world!";
     
    // Now its value is "Hello, world!"

    Do this whenever possible:

    let message = "Hello, world!";
  • Lastly, prefer const when possible: If you don’t need to reassign a variable, use const. This makes it clear to other developers that the value will not change, which can prevent errors.

Conclusion

Variables are a fundamental concept in any programming language, and JavaScript is no exception. Knowing when to use var, let, and const, and how to manage the scope of your variables is essential for writing cleaner, less error-prone code. By following these best practices, you’ll ensure that your code is efficient, clear, and easy to maintain.

I hope this post has helped you better understand how variables work in JavaScript. If you have any questions or suggestions, feel free to reach me out. I’m here to help you improve on your journey as a developer!