Understanding Variables and Data Types in JavaScript
Containers
The programming world may seem virtual, but it often draws inspiration from real life. The way we think in programming and even the smallest actions we perform are often imitations of real-world activities.
For example, think about organizing things in your home. Whether it is a desk, bookshelf, kitchen shelf, or a simple pen stand, all of them are containers or designated spaces where you store specific items. Each holder or container is designed to hold certain things.
You would not use a pen stand to store paper clips, nor would you use your desk drawer to store kitchen items like salt, spices, or pepper. This is because both the container and the items have their own designated purpose.
Similarly, in programming we store values or information. These values may be text, numbers, a combination of both, or other specialized forms of data. The places where we store these values—just like real-life containers—have predefined types.
For example, text-based information is stored in one type of container, while numeric values are stored in another. Let us see how this works in a real programming environment.
Variables
Variables are places in memory allocated to your program from the system’s Random Access Memory (RAM). In these locations, you can store text, numbers, or other customized types of data in their raw form.
Each variable has a name, also known as an identifier, which is used to specify the exact memory location you want to access. Using this name, you can read data from the variable or write new data into it.
World of JavaScript
Like any other programming language, JavaScript provides programmers with multiple ways to declare variables and store data in them.
var variables
First, we will look at var. In JavaScript, var is a reserved keyword used to declare variables, as shown below.
var name = "Violet";
Although once very popular, this way of declaring variables is no longer recommended. In modern production codebases, the use of var is often discouraged.
var comes with several confusing quirks, such as allowing variables to be used before they are declared, along with other behaviors that make it less suitable for serious development.
let variables
Instead of var, nowadays let is the preferred way of declaring variables in JavaScript. Variables declared with let can only be used after they are declared, and they do not have the surprising quirks associated with var.
The syntax for declaring a let variable is the same as var; you simply use the let keyword as shown below.
One notable difference between var and let is that var allows the same variable to be declared multiple times, whereas doing so with let results in a syntax error.
var name = "Rohit";
var name = "Priya"; //No problem here
let age = 20;
let age = 31; //This will throw an error
let city = "Pune";
city = "Mumbai"; //This is valid
Sometimes you may not want the value of a variable to change after it is declared. A common example is mathematical constants, such as the value of PI (3.1415...).
In such cases, you can use the const keyword to declare constants.
const variables
const variables are also known as constants .. once you declare a variable using const keyword that variable will never again be overwritten. If you try to overwrite a const variable it will throw you an error at runtime.
const PI = 3.1415;
PI = 500; //Error
Data Types
Before you start creating variables in JavaScript with this newly acquired knowledge, there is one more thing you should know: every variable has a type. But what does that mean?
It’s nothing complicated. It simply means that each variable has a property that indicates what kind of value is stored in it.
For example, names or text are stored as string type values, while numbers—both integers and decimal values—are stored as number type values.
Strings or text values are written within double quotes " ", while numbers are written directly as they are.
There are many more types of data that you can store in a JavaScript variable, such as arrays, objects, and others. But for now, it is enough to understand that data has a type, and that type matters.
Think about it this way: a variable named address should store an address, right? Not a contact number. Although JavaScript technically allows you to change the type of a variable, doing so is not recommended in real-world software development and is often considered a bug or undesired behavior.
Conclusion
This was a basic introduction to variables and data types in JavaScript. Hopefully, it was simple and intuitive enough to help you understand what variables and data types are, and the basics of how they are used in JavaScript.



