DEV Community

Cover image for Understanding Variables and Data Types in JavaScript
Souvik Guha Roy
Souvik Guha Roy

Posted on

Understanding Variables and Data Types in JavaScript

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, and const
  • Primitive data types (string, number, boolean, null, undefined, etc.)
  • The basic differences between var, let, and const
  • 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;
Enter fullscreen mode Exit fullscreen mode

Here:

  • count is the variable name
  • 1 is 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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:

  • let
  • const
  • var

Example:

let count = 1;
Enter fullscreen mode Exit fullscreen mode

This statement does two things:

  1. Declares a variable called count
  2. Assigns the value 1 to 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);
Enter fullscreen mode Exit fullscreen mode

Output:

3
Enter fullscreen mode Exit fullscreen mode

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";
Enter fullscreen mode Exit fullscreen mode

2. Number

Numbers can be integers or decimals.

let marks = 95;
let price = 99.99;
Enter fullscreen mode Exit fullscreen mode

3. Boolean

A boolean value can only be true or false.

let isLoggedIn = true;
let isAdmin = false;
Enter fullscreen mode Exit fullscreen mode

4. Undefined

When a variable is declared but no value is assigned, its value becomes undefined.

let x;
Enter fullscreen mode Exit fullscreen mode

5. Null

null means a variable intentionally has no value.

let data = null;
Enter fullscreen mode Exit fullscreen mode

6. BigInt

Used for very large numbers that cannot be represented by normal numbers.

let bigNumber = 12345678901234567890n;
Enter fullscreen mode Exit fullscreen mode

7. Symbol

A Symbol creates a unique value, often used for object properties.

let id = Symbol("id");
Enter fullscreen mode Exit fullscreen mode

Objects (Reference Type)

Objects store multiple values together.

Example:

let user = {
  name: "Kunal",
  age: 21
};
Enter fullscreen mode Exit fullscreen mode

⚠️ 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 const by default
  • Use let when 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
Enter fullscreen mode Exit fullscreen mode

let (Block Scope)

let works inside a block {}.

{
  let age = 25;
  console.log(age); // Works
}

console.log(age); // Error
Enter fullscreen mode Exit fullscreen mode

const (Block Scope)

const also works inside a block {}.

{
  const PI = 3.14;
  console.log(PI); // Works
}

console.log(PI); // Error
Enter fullscreen mode Exit fullscreen mode

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)