Variables & Data Types

Date : 02 Apr 2025
Author : DeepSeek
Tags :

Variables and data types are fundamental concepts in JavaScript. They allow you to store and manipulate data, which is essential for building dynamic and interactive web applications. In this blog, we’ll explore the differences between var, let, and const, as well as the distinction between primitive and reference data types.


Declaring Variables: var, let, and const

JavaScript provides three ways to declare variables: var, let, and const. Each has its own use cases and behaviors.

1. var

var name = "DeepSeek";
name = "JavaScript"; // Reassignment is allowed
console.log(name); // Output: JavaScript

2. let

let age = 25;
age = 26; // Reassignment is allowed
console.log(age); // Output: 26

3. const

const PI = 3.14;
// PI = 3.14159; // This will throw an error (reassignment not allowed)

const user = { name: "DeepSeek" };
user.name = "JavaScript"; // This is allowed (modifying properties)
console.log(user.name); // Output: JavaScript

Primitive vs. Reference Data Types

JavaScript data types can be categorized into two groups: primitive and reference types.

1. Primitive Data Types

Primitive types are immutable (cannot be changed) and are stored directly in memory. They include:

let name = "DeepSeek"; // String
let age = 25;          // Number
let isDeveloper = true; // Boolean
let unknown;           // Undefined
let empty = null;      // Null
let id = Symbol("id"); // Symbol
let bigNumber = 123456789012345678901234567890n; // BigInt

2. Reference Data Types

Reference types are mutable and are stored as references in memory. They include:

let user = { name: "DeepSeek", age: 25 }; // Object
let numbers = [1, 2, 3];                  // Array
function greet() {                        // Function
    console.log("Hello!");
}

Key Differences Between Primitive and Reference Types

FeaturePrimitive TypesReference Types
MutabilityImmutable (cannot be changed)Mutable (can be changed)
StorageStored directly in memoryStored as a reference in memory
ComparisonCompared by valueCompared by reference (memory address)
Examplelet a = 10; let b = a;let obj1 = {}; let obj2 = obj1;

Example: Primitive vs. Reference Behavior

// Primitive Type Example
let a = 10;
let b = a; // `b` gets a copy of the value of `a`
b = 20;
console.log(a); // Output: 10 (unchanged)

// Reference Type Example
let obj1 = { name: "DeepSeek" };
let obj2 = obj1; // `obj2` gets a reference to the same object as `obj1`
obj2.name = "JavaScript";
console.log(obj1.name); // Output: JavaScript (changed)

Best Practices for Using Variables and Data Types

  1. Use const by default: If you don’t need to reassign a variable, use const to make your code more predictable.
  2. Use let for reassignable variables: If you need to reassign a variable, use let.
  3. Avoid var: var has quirks like hoisting and function-scoping, which can lead to bugs. Prefer let and const.
  4. Understand mutability: Be aware of how primitive and reference types behave differently, especially when passing them to functions or assigning them to new variables.

Conclusion

Understanding variables and data types is crucial for writing effective JavaScript code. By mastering the differences between var, let, and const, as well as the behavior of primitive and reference types, you’ll be better equipped to build robust and maintainable applications. Start applying these concepts in your code today!


Further Reading

Table of Contents