JavaScript Variables and Data Types – A Beginner’s Guide

🔹 What is a Variable?

A variable is a named container that stores data that can be used and modified later.

In modern JavaScript, you can declare variables using:

  • var (old, avoid using it)

  • let (block-scoped, preferred)

  • const (block-scoped, for constants)

Example:

javascriptCopyEditlet name = "Abhay";  // can change later
const pi = 3.14;     // constant value

🔹 Rules for Naming Variables

  • Names can contain letters, digits, $, and _

  • Must begin with a letter, $, or _

  • Case-sensitive (Name and name are different)

  • Cannot be reserved keywords (let, if, class, etc.)

🔹 JavaScript Data Types

JavaScript has two categories of data types:

🧾 1. Primitive Data Types

These hold single values and are immutable.

Data TypeExample
String"Hello"
Number42, 3.14
Booleantrue, false
Nullnull (intentional empty value)
Undefinedundefined (variable declared but no value assigned)
SymbolSymbol('id') (for unique identifiers)
BigInt1234567890123456789012345678901234567890n (for very large numbers)
javascriptCopyEditlet firstName = "Abhay";     // String
let age = 22;                // Number
let isStudent = true;        // Boolean
let address = null;          // Null
let score;                   // Undefined
let id = Symbol("unique");   // Symbol

📦 2. Non-Primitive (Reference) Data Types

These store collections of values.

TypeExample
Object{name: "Abhay", age: 22}
Array["JS", "HTML", "CSS"]
Functionfunction greet() {}
javascriptCopyEditlet person = {
  name: "Abhay",
  age: 22,
};

let skills = ["JS", "HTML", "CSS"];

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

🔍 typeof Operator

To check the type of a variable, use typeof:

javascriptCopyEditconsole.log(typeof "Hello");     // string
console.log(typeof 42);          // number
console.log(typeof true);        // boolean
console.log(typeof undefined);   // undefined
console.log(typeof null);        // object (quirk in JS)
console.log(typeof person);      // object

🔄 Dynamic Typing

JavaScript is dynamically typed, which means:

  • You don’t need to declare a type.

  • Variables can change types at runtime.

javascriptCopyEditlet value = 42;      // Number
value = "Forty-two"; // Now a String

✅ Tips for Beginners

  • Always use let or const (avoid var unless necessary).

  • Use const when you don’t plan to reassign a variable.

  • Learn how types interact with each other (type coercion can be tricky!).

  • Use typeof to check a variable's type:

javascriptCopyEditconsole.log(typeof 123);       // "number"
console.log(typeof "hello");   // "string"

🎓 Summary

KeywordUsed For
letDeclaring block-scoped variables
constDeclaring constants
varDeclaring function-scoped variables (legacy)
TypeDescription
StringText
NumberNumbers (integers and floats)
Booleantrue or false
NullEmpty or unknown value
UndefinedVariable declared but no value
ObjectComplex data structures
ArrayList of values

Devsync

0
Subscribe to my newsletter

Read articles from Abhay Ganesh Bhagat directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Abhay Ganesh Bhagat
Abhay Ganesh Bhagat