JavaScript Variables and Data Types (Beginner’s Guide)
JavaScript often looks easy at first. You write a few lines of code, run it, and everything seems to work.
But very quickly you realize that almost everything in JavaScript depends on variables and data types.
If you skip these fundamentals, your code can quickly turn into a guessing game where you end up using console.log() everywhere just to understand what’s happening.
So before moving deeper into JavaScript, it's important to understand how variables and data types work.
Topics Covered
In this guide we will learn:
- What variables are and why they are needed
- How to declare variables using
var,let, andconst - Primitive data types (
string,number,boolean,null,undefined, etc.) - The basic differences between
var,let, andconst - What scope means in JavaScript (beginner-friendly explanation)
Variables
When a child is born, they are given a name. Throughout their life people refer to them by that name unless it is changed.
Variables in JavaScript work in a similar way.
A variable is simply a name used to store a value. Whenever we need that value, we can access it by using the variable’s name.
For example:
let count = 1;
Here:
-
countis the variable name -
1is the value stored in that variable
Whenever we use count, JavaScript will refer to the value 1.
How to Name Variables
Just like human names, variable names should be meaningful and easy to understand.
Technically, you could write something like:
pneumonoultramicroscopicsilicovolcanoconiosis
This is actually a valid variable name — but it’s far too long and confusing.
Good variable names should be short and descriptive.
Examples:
clickButton
addNumbers
sumOfNumbers
userAge
totalPrice
A good variable name should be self-explanatory so that someone reading your code can easily understand what it represents.
Declaring and Assigning Variables
To create a variable in JavaScript, we use one of these keywords:
letconstvar
Example:
let count = 1;
This statement does two things:
-
Declares a variable called
count -
Assigns the value
1to it
The = symbol is called the assignment operator, which assigns a value to a variable.
Accessing a Variable
Once a variable has been created and assigned a value, we often want to use or display that value in our program.
One common way to do this is using console.log().
Example:
let count = 1;
console.log(count + 2);
Output:
3
Here JavaScript takes the value stored in count and adds 2 to it.
Data Types
In JavaScript, data types define what kind of value a variable can store.
Think of data types like containers in real life:
- A water bottle holds water
- A wallet holds money
- A folder holds documents
Similarly, different data types hold different kinds of values.
JavaScript data types are broadly divided into two categories:
1. Primitive Data Types
- String
- Number
- Boolean
- Undefined
- Null
- BigInt
- Symbol
2. Reference Data Types
- Object
- Array
- Function
For now, let's focus on primitive data types.
Primitive Data Types
1. String
A string represents text and is written inside quotes.
let message = "Like this blog";
2. Number
Numbers can be integers or decimals.
let marks = 95;
let price = 99.99;
3. Boolean
A boolean value can only be true or false.
let isLoggedIn = true;
let isAdmin = false;
4. Undefined
When a variable is declared but no value is assigned, its value becomes undefined.
let x;
5. Null
null means a variable intentionally has no value.
let data = null;
6. BigInt
Used for very large numbers that cannot be represented by normal numbers.
let bigNumber = 12345678901234567890n;
7. Symbol
A Symbol creates a unique value, often used for object properties.
let id = Symbol("id");
Objects (Reference Type)
Objects store multiple values together.
Example:
let user = {
name: "Kunal",
age: 21
};
⚠️ Note: In JavaScript, the data type of an array is also object.
Difference Between var, let, and const
| Feature | var | let | const |
|---|---|---|---|
| Scope | Function scope | Block scope {}
|
Block scope {}
|
| Redeclaration | Allowed | Not allowed | Not allowed |
| Change value | Allowed | Allowed | Not allowed |
| Initialization | Optional | Optional | Required |
| Modern usage | Mostly avoided | Commonly used | Commonly used |
In modern JavaScript:
- Use
constby default - Use
letwhen the value needs to change - Avoid
var
Understanding Scope
Scope simply means where a variable can be accessed in your code.
var (Function Scope)
A variable declared with var is only accessible inside the function where it was declared.
function example() {
var message = "Hello";
console.log(message); // Works
}
example();
console.log(message); // Error
let (Block Scope)
let works inside a block {}.
{
let age = 25;
console.log(age); // Works
}
console.log(age); // Error
const (Block Scope)
const also works inside a block {}.
{
const PI = 3.14;
console.log(PI); // Works
}
console.log(PI); // Error
Final Thoughts
Variables and data types are the foundation of JavaScript.
If you understand these concepts well, learning more advanced topics like:
- functions
- arrays
- objects
- asynchronous JavaScript
will become much easier.
Take time to practice writing small examples and experimenting with console.log() to see how values change.
Master these basics, and your JavaScript journey will become much smoother.
Top comments (0)