1. let — The Modern Variable
let was introduced in ES6 (2015) and is now the preferred way to declare variables that can change their value.
let age = 20;
console.log(age); // 20
age = 21; // ✅ you can update it
console.log(age); // 21
Why let over var?
The key difference is scope — let is block-scoped, meaning it only exists within the {} block it was defined in.
if (true) {
let message = "Hi!";
console.log(message); // ✅ works here
}
console.log(message); // ❌ ReferenceError — not accessible outside
With var, the same code would work outside the block — which often causes unexpected bugs. let keeps things predictable and safe.
2. const — The Constant
Use const when the value should never change after it's set.
const PI = 3.14159;
console.log(PI); // 3.14159
PI = 3; // ❌ TypeError — you cannot reassign a const
Must be initialised immediately
Unlike let, you cannot declare a const without giving it a value.
const country; // ❌ SyntaxError
const country = "India"; // ✅ correct
When to use let vs const?
A simple rule of thumb:
Use
constby default. Switch toletonly if you know the value will change.
const userName = "Karthick"; // won't change — use const
let score = 0; // will change as user plays — use let
3. JavaScript Data Types
Every value in JavaScript has a type. Understanding types helps you know what you can do with a value.
JavaScript has 8 primitive types and 1 non-primitive type (object). Let's look at the most important ones.
String
A string is text. Wrap it in single quotes '...', double quotes "...", or backticks `...`.
let name = "Priya";
let city = 'Chennai';
let greeting = `Hello, ${name}!`; // template literal — can embed variables
console.log(greeting); // Hello, Priya!
Number
JavaScript has just one number type — it handles both whole numbers and decimals.
let age = 25;
let price = 49.99;
let temperature = -5;
You can also do math with numbers:
let total = 100 + 50; // 150
let half = 100 / 2; // 50
let squared = 4 ** 2; // 16
BigInt
Use BigInt when you need whole numbers larger than what Number can safely handle.
JavaScript's Number type has a maximum safe value:
console.log(Number.MAX_SAFE_INTEGER); // 9007199254740991
Beyond this, regular numbers lose precision. BigInt solves this — just add n at the end of the number:
let bigNumber = 9007199254740991n;
let bigger = 99999999999999999999n; // no precision loss!
console.log(typeof bigNumber); // bigint
BigInt only works with other BigInts — you can't mix them with regular numbers:
let a = 100n + 200n; // ✅ 300n
let b = 100n + 5; // ❌ TypeError
Boolean
A boolean is simply true or false. Used a lot in conditions.
let isLoggedIn = true;
let hasDiscount = false;
if (isLoggedIn) {
console.log("Welcome back!");
}
Undefined
A variable that has been declared but not yet given a value is undefined.
let score;
console.log(score); // undefined
Null
null is an intentional "empty" value. You use it when you want to explicitly say "this has no value".
let selectedItem = null; // nothing selected yet
💡
undefinedvsnull:undefinedmeans a value was never assigned.nullmeans you intentionally set it to "nothing".
Object
An object groups related data together using key-value pairs.
let student = {
name: "Karthick",
age: 21,
course: "Data Mining"
};
console.log(student.name); // Karthick
console.log(student["age"]); // 21
Array
An array is a list of values, stored in order.
let fruits = ["apple", "mango", "banana"];
console.log(fruits[0]); // apple (index starts at 0)
console.log(fruits.length); // 3
Checking the Type of a Value
Use the typeof operator to find out what type a value is:
console.log(typeof "hello"); // string
console.log(typeof 42); // number
console.log(typeof true); // boolean
console.log(typeof undefined); // undefined
console.log(typeof null); // object ← known JavaScript quirk!
console.log(typeof {}); // object
console.log(typeof []); // object
Quick Recap
| Keyword | Can reassign? | Block-scoped? | Use when... |
|---|---|---|---|
var |
✅ Yes | ❌ No | Legacy code only |
let |
✅ Yes | ✅ Yes | Value will change |
const |
❌ No | ✅ Yes | Value stays fixed |
| Type | Example |
|---|---|
| String | "Hello" |
| Number |
42, 3.14
|
| Boolean |
true, false
|
| Undefined | let x; |
| Null | let x = null; |
| Object | { name: "Priya" } |
| Array | [1, 2, 3] |
| BigInt | 9999999999999999n |
Top comments (0)