Skip to main content

Command Palette

Search for a command to run...

The Ultimate Beginner's Guide to JavaScript Variables and Data Types

Updated
5 min read
The Ultimate Beginner's Guide to JavaScript Variables and Data Types
N

MCA graduate | Software developer documenting my learning journey and sharing beginner-friendly tech concepts.

Starting your programming journey can feel like stepping into a foreign country. Everyone is speaking a strange language, and the rules seem entirely new. But here is a secret: programming is just a way to solve real-world problems using logic.

Today, we are going to break down the absolute fundamentals of JavaScript: Variables, Data Types, and Scope. By the end of this article, you will not only understand how they work, but you will also write your very first script.

1. What is a Variable? (The Magic Box Analogy)

Imagine you are moving to a new apartment. You have a lot of stuff—books, clothes, and kitchen utensils. To keep things organised, you pack them into cardboard boxes and write a label on the outside of each box.

In JavaScript, a variable is exactly like that labeled box. It is a container used to store information (data) so you can refer back to it, use it, or change it later.

Why do we need them?

Without variables, computer programs would have a terrible memory. If you type your name into a website, the website needs a "box" to hold your name while you browse. Otherwise, it would instantly forget who you are.

2. How to Declare Variables (var, let, and const)

In JavaScript, creating a variable is called declaring a variable. Think of it as building the box and slapping a label on it.

JavaScript gives us three different keywords to create these boxes: var, let, and const.

The Syntax

To declare a variable, you write the keyword, give it a unique name (the label), and use the equal sign (=) to put something inside it.

let totalScore = 100;

Changing Values: let vs. const

The biggest practical difference between these keywords is whether the data inside the box can be changed later.

  • let allows change: Use this when you expect the value to alter over time (like a game score or your age).

  • const is constant: Short for "constant," this creates a locked box. Once you put something inside, you can never change it. Use this for things that stay the same (like your date of birth or the value of Pi).

// Using let (The value can change)
let currentWeight = 75;
currentWeight = 72; // Perfectly fine!

// Using const (The value is locked)
const dateOfBirth = "15-06-2000";
dateOfBirth = "20-01-1999"; // ERROR! JavaScript will stop running here.

3. Meet the Neighbors: Primitive Data Types

What can you actually put inside these boxes? JavaScript recognizes different types of information, known as Data Types. Let’s look at the five most common "primitive" types you will use every single day.

Data Type

What it represents

Simple Example

Code Snippet

String

Textual data (always wrapped in quotes)

A person's name

let name = "Ankit";

Number

Numeric values (integers or decimals)

An age or price

let age = 25;

Boolean

A simple True or False switch

On/Off states

let isLoggedIn = true;

Null

An intentional completely empty box

Saying "this has no value"

let currentJob = null;

Undefined

An empty box that hasn't been set up yet

A box with no content inside

let futureProject;

💡 Quick Note: A String can use single quotes ('...') or double quotes ("..."). Just make sure you match them up correctly!

4. The Core Differences: var vs. let vs. const

You might be wondering: "What about var?" var is the oldest way to declare variables in JavaScript. However, it has some quirky behaviors that often caused bugs for developers. Modern JavaScript introduced let and const to fix those issues.

Here is a quick cheat sheet to remember how they differ

  • var: Old school, flexible, but can cause unpredictable bugs. Avoid using it in modern code.

  • let: Modern, can be reassigned (changed), safe to use.

  • const: Modern, cannot be reassigned, safe to use. Pro tip: Default to using const unless you know the value needs to change later.

5. What is Scope? (A Beginner-Friendly Explanation)

Think of Scope as the visibility of your variables. It determines which parts of your code are allowed to look inside your boxes.

Imagine a secure apartment building:

  • Global Scope: Anything placed in the main lobby is accessible to everyone in the building.

  • Local/Block Scope: Anything placed inside a specific apartment room is private. People in the lobby cannot see it or touch it.

In JavaScript, code blocks are defined by curly braces { }. Variables declared inside these braces using let or const are locked inside that specific room.

// This variable is in the Global Lobby
let globalGreeting = "Hello World!"; 

{
    // This variable is locked inside this Local Room
    let localSecret = "I love coding"; 
    console.log(localSecret); // Works perfectly!
}

console.log(globalGreeting); // Works perfectly!
console.log(localSecret);    // ERROR! The lobby cannot see into the private room.