Skip to main content

Command Palette

Search for a command to run...

Understanding Variables and Data Types in JavaScript

Updated
5 min readView as Markdown
Understanding Variables and Data Types in JavaScript
D
CS student focused on backend & OS fundamentals. Building real projects and sharing practical learning.

variable

A variable is a named reference to a value stored in memory.

in simpler term we say A variable is a named box that stores a value so the program can use it later.

Example:

let name = "Dushyant";
console.log(name);

Here:

  • name → variable

  • "Dushyant" → stored value

Declaration of variable

in JS variable can be declared using three keyword

  1. var
var age = 21;
console.log(age);

var was used in older JS , but today dveloper rarely used it . try to avoid it

2 . let

let city = "Delhi";
console.log(city);

let allows you to change the value later.

Example:

let score = 10;
score = 20;

console.log(score);

3. const

const country = "India";
console.log(country);

const means the value cannot be reassigned.

Example:

const pi = 3.14;
pi = 3.14159; // Error

Datatype

A datatype is simply the type of value a variable holds.

in Js datatypes are two types -

  1. Primitive Data Types

  2. Non Primitivr Data Types

primitive data types

basically they stors single simple value directly. they are immutable

types:

  1. string

  2. number

  3. boolean

  4. undefined

  5. null

  6. BigInt

  7. Symbol

In JavaScript, data types are mainly divided into two categories:

  1. Primitive Data Types

  2. Non-Primitive (Reference) Data Types

If you don’t understand this difference early, you’ll get confused later with objects, arrays, and functions, so learn it properly.


1. Primitive Data Types

Primitive data types store a single simple value directly.

They are immutable, meaning the original value itself cannot be changed.

Types of Primitive Data in JavaScript

  1. String – text data
let name = "Dushyant";
  1. Number – numeric values
let age = 21;
  1. Boolean – true or false
let isStudent = true;
  1. Undefined – variable declared but not assigned
let value;
console.log(value); // undefined
  1. Null – intentional empty value
let data = null;
  1. BigInt – very large numbers
let bigNumber = 12345678901234567890n;
  1. Symbol – unique identifiers
let id = Symbol("id");

Key Characteristics

  • Store single values

  • Stored directly in memory

  • Immutable


Non Primitive datatype

it basically store multiple vslues or complete data structure , and the vsrisble store a reference memory to that dsts instesd of actual value

types

  1. Object

  2. Array

  3. Functionn

Object

let person = {
  name: "Dushyant",
  age: 21
};

we will discuss abt this in next blog

Array

let numbers = [1, 2, 3, 4];

we will discuss it in next blog

Function

function greet() {
  console.log("Hello");
}

we had discull abt this, in case if you miss

https://youtu.be/_ZjkifF7QD8

## summary for datatypes
Primitive Non-Primitive
Stores single value Stores multiple or complex values
Stored directly in memory Stored as reference
Immutable Mutable
Example: number, string Example: object, array

why do we require 3 different variable:

in engineerinng everyrhing happenn fro a reason

1. No Block Scope

var ignores blocks like {}.
That means a variable created inside a block can still leak outside.

if (true) {
  var x = 10;
}

console.log(x); // 10 (still accessible)

With let, this would throw an error.

if (true) {
  let x = 10;
}

console.log(x); // Error

This behavior causes unexpected variable leaks.

2. Redeclaration is Allowed

var allows redeclaring the same variable, which can accidentally overwrite values.

var name = "Dushyant";
var name = "Rahul";

console.log(name); // Rahul

With let, this would stop you:

let name = "Dushyant";
let name = "Rahul"; // Error

3. Hoisting Confusion

var is hoisted, meaning JavaScript moves the declaration to the top internally.

Example:

console.log(a);
var a = 10;

Output:

undefined

This confuses beginners because the variable appears to exist before it is defined.

4. Global Pollution

When you declare a var variable globally, it becomes part of the global object and can create conflicts.

var data = 100;

Large applications can break because multiple scripts might accidentally use the same variable name.

Suggestion: You are going to be a developer so uses var rarely

sice today common practice are

  • const → default choice

  • let → when value needs to change

  • var → avoid


scope

sccope is a region of a program where a variabel can be accessed or used , basically scope decided where you variable exists and where it can be used.

let name = "Dushyant";

function greet() {
  console.log(name);
}

greet();

Here the variable name is accessible inside the function because it is defined outside it.

Types of Scope

1. Global Scope

Variables declared outside any function or block.

let a = 5;

Accessible everywhere in the program.


2. Function Scope

Variables declared inside a function.

function test() {
  let b = 10;
}

b exists only inside the function.


3. Block Scope

Variables declared inside {} using let or const.

if (true) {
  let c = 20;
}

c exists only inside that block.


IF YOU STILL DID NOT GET IT HERE IS MORE SIMPLER

https://youtu.be/4BMi1UiHlRY