Understanding Variables (var, let, const) and Data Types in JavaScript
learn with #vibin-charles

Understanding Variables (var, let, const) and Data Types in JavaScript

Before jumping into frameworks, a strong grasp of JavaScript variables and data types is essential. These are the building blocks of any JavaScript program. Though it is a way simple and basic, we need to know where to use which. Mastering in it will give you better idea on it.


1️⃣ Declaring Variables: var, let, and const

JavaScript provides three ways to declare variables:

✅ var (Function-scoped, Can be Reassigned)

🔹 Introduced in ES5 and earlier.

🔹 Function-scoped: Accessible only within the function where it is declared.

🔹 Can be redeclared and updated, which can lead to unexpected behaviors.

var name = "Alice";
var name = "Bob"; // Redeclaring is allowed
console.log(name); // Output: Bob
        

✅ let (Block-scoped, Can be Reassigned)

🔹 Introduced in ES6.

🔹 Block-scoped: Only accessible within the block {} where it is declared.

🔹 Can be updated but not redeclared within the same scope.

let age = 25;
age = 30; // Allowed
console.log(age); // Output: 30
        

✅ const (Block-scoped, Cannot be Reassigned)

🔹 Also introduced in ES6.

🔹 Block-scoped like let.

🔹 Cannot be reassigned after declaration.

const country = "USA";
country = "Canada"; // ❌ Error: Assignment to constant variable
        

💡 Best Practice: Use const by default unless you need to update the variable, then use let. Avoid var to prevent scoping issues.


2️⃣ Data Types in JavaScript

JavaScript has two main categories of data types:

🟢 Primitive Data Types (Stored by Value)

These types hold single values and are immutable (cannot be changed).

  1. String → "Hello, World!"
  2. Number → 42, 3.14
  3. Boolean → true, false
  4. Undefined → A variable declared but not assigned (let x;)
  5. Null → An intentional empty value (let x = null;)
  6. Symbol → Unique and immutable value (used in advanced cases)
  7. BigInt → Large numbers beyond Number limits (9007199254740991n)

Example:

let message = "Hello"; // String
let count = 10;        // Number
let isActive = true;   // Boolean
        

🟡 Reference Data Types (Stored by Reference)

These types hold complex data structures and can be modified.

  1. Objects → { name: "Alice", age: 25 }
  2. Arrays → [1, 2, 3, 4]
  3. Functions → function greet() {}

Example:

let person = { name: "Alice", age: 25 }; // Object
let numbers = [1, 2, 3]; // Array
        

💡 Key Difference:

  • Primitive types are stored by value (changing a copy doesn't affect the original).
  • Reference types are stored by reference (modifying a copy affects the original).


3️⃣ Choosing the Right Variable and Data Type

Use const for values that won’t change.

Use let for values that need to be reassigned.

Use objects and arrays when storing collections of data.


Final Thoughts 💡

Mastering variables and data types is crucial for writing clean and bug-free JavaScript code. As you move forward, understanding these fundamentals will make handling complex data structures in frameworks much easier!

👉 Next, we’ll explore Functions, Scope, and Closures! Stay tuned.

💬 What’s the most confusing part about JavaScript variables for you? Let’s discuss in the comments! 👇

#JavaScript #WebDevelopment #JSBeforeFrameworks #Coding #jsbasics #javascriptbasics

#fullstackdevelopment

Thirumoorthi M

Junior Full Stack Developer | MERN & PERN Stack Enthusiast

3mo

Useful tips

To view or add a comment, sign in

More articles by Vibin Charles B

Insights from the community

Others also viewed

Explore topics