Master JavaScript Variables and Data Types | var, let, const Explained with Examples

Javascript

JavaScript Variables and Data Types – Beginner’s Complete Guide

When you start learning JavaScript, the first thing you must understand is how to store and manage data. This is where variables and data types come into play. They are the foundation of every JavaScript program.

In this guide, you’ll learn everything about JavaScript variables, var, let, and const, along with all data types in JavaScript — both primitive and non-primitive.

🔹 What Are Variables in JavaScript?

A variable is like a container that stores data. You can assign, change, or access the stored value anytime during your program.

let name = "Lokesh"; let age = 25; let isDeveloper = true; 

Here, name, age, and isDeveloper are variables that hold string, number, and boolean data respectively.

🔸 Declaring Variables: var, let, and const

JavaScript allows you to declare variables in three ways — using var, let, or const.

KeywordScopeReassignableBest Use
varFunction Scoped✅ YesOld style, avoid using
letBlock Scoped✅ YesUse for changeable values
constBlock Scoped❌ NoUse for fixed values
var city = "Jaipur"; let country = "India"; const PI = 3.14159; 

🔹 Rules for Naming Variables

  • Names must start with a letter, underscore (_) or dollar sign ($)
  • Cannot start with a number (❌ 1name)
  • Case sensitive — age and Age are different
  • Use meaningful names like userName or totalAmount

🔸 JavaScript Data Types

Data types define the kind of values a variable can hold. JavaScript has two main categories of data types:

1️⃣ Primitive Data Types

Type Example Description String "Lokesh" Textual data Number 25 Numeric value Boolean true True/false logic Undefined let x; Declared but no value assigned Null let y = null; Empty or intentional absence of value Symbol Symbol("id") Unique identifier BigInt 12345678901234567890n Large integer values
let name = "Lokesh"; let age = 25; let isActive = true; let salary = null; let id = Symbol("userId"); let bigNumber = 12345678901234567890n; 

2️⃣ Non-Primitive (Reference) Data Types

Type Example Description Object { name: "Lokesh", age: 25 } Key-value pairs Array [10, 20, 30] Ordered list Function function greet() {} Block of reusable code
let person = { name: "Lokesh", age: 25 }; let colors = ["red", "green", "blue"]; function greet() { console.log("Hello, world!"); } 

🔍 Checking Data Type with typeof Operator

You can easily find out what type of data a variable holds using the typeof keyword.

console.log(typeof "Lokesh"); // string console.log(typeof 25); // number console.log(typeof true); // boolean console.log(typeof {}); // object console.log(typeof []); // object console.log(typeof null); // object 

💡 Common Mistakes to Avoid

  • ❌ Using var everywhere — use let and const instead
  • ❌ Confusing null and undefined
  • ❌ Reassigning const variables
  • ✅ Use typeof to debug your data types

🚀 Real-World Example

const user = { name: "Lokesh", age: 25, isPremium: false }; if (user.isPremium) { console.log("Welcome, Premium User!"); } else { console.log("Upgrade to Premium for more features!"); } 

✅ Output: Upgrade to Premium for more features!

📚 Quick Summary

  • Variables store values in memory.
  • Use let for changeable values and const for constants.
  • JavaScript has 7 primitive and 3 non-primitive data types.
  • typeof helps you identify the data type.

🎯 SEO Tip:

If you’re searching for “difference between var let const in JavaScript” or “JavaScript data types with examples,” this guide covers all the essential explanations and examples for beginners.

📚 FAQ (Frequently Asked Questions)

Q1: What is the difference between var, let, and const?

var is function-scoped, while let and const are block-scoped. Use let when the value changes and const when it doesn’t.

Q2: How many data types are there in JavaScript?

There are 7 primitive and 3 non-primitive (object, array, function) data types in JavaScript.

Q3: Is null an object in JavaScript?

Technically, typeof null returns “object”, but it’s a long-standing JavaScript quirk.

Q4: What is the difference between undefined and null?

undefined means a variable has been declared but not assigned a value, while null means an intentional empty value.

➡️ Next Lesson:

In the next part, we’ll learn about JavaScript Operators — arithmetic, comparison, logical, and assignment operators with examples.

Keep coding and stay consistent 💪

Share:

JavaScript Hoisting Explained (var, let, const)

JavaScript Hoisting moves variable and function declarations to the top of their scope. Let’s understand how hoisting works with var, let, and const using simple examples.

Understanding JavaScript Closures with Simple Examples

JavaScript closures allow functions to remember variables from their parent scope even after the parent function has finished. Let’s explore closures with simple and real-life examples.

JavaScript Promises Explained with Real-Life Examples

Understand JavaScript Promises with real-world examples. Learn how to use .then(), .catch(), and async/await for handling asynchronous operations effectively.

Master JavaScript Variables and Data Types | var, let, const Explained with Examples

Understand JavaScript variables and data types in a beginner-friendly way. Learn the difference between var, let, and const, and explore primitive and non-primitive data types with real examples.

Master JavaScript Conditional Statements (if, else, switch) with Examples

Understand JavaScript conditional statements and control program flow using if, else, and switch. Learn with examples and practical coding tips for beginners.

Understanding Variables and Data Types in JavaScript

Learn about JavaScript variables and data types — the foundation of every program. Understand var, let, const, and explore primitive and non-primitive data types with real-world examples.

What is JavaScript and Why Is It Important?

JavaScript is a powerful programming language that brings interactivity and life to web pages. It is one of the most essential skills for any web developer.