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
andname
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 Type | Example |
String | "Hello" |
Number | 42 , 3.14 |
Boolean | true , false |
Null | null (intentional empty value) |
Undefined | undefined (variable declared but no value assigned) |
Symbol | Symbol('id') (for unique identifiers) |
BigInt | 1234567890123456789012345678901234567890n (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.
Type | Example |
Object | {name: "Abhay", age: 22} |
Array | ["JS", "HTML", "CSS"] |
Function | function 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
orconst
(avoidvar
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
Keyword | Used For |
let | Declaring block-scoped variables |
const | Declaring constants |
var | Declaring function-scoped variables (legacy) |
Type | Description |
String | Text |
Number | Numbers (integers and floats) |
Boolean | true or false |
Null | Empty or unknown value |
Undefined | Variable declared but no value |
Object | Complex data structures |
Array | List of values |
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
