DEV Community

Karthick Narayanan
Karthick Narayanan

Posted on

JavaScript Part 2: let, const & Data Types

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
Enter fullscreen mode Exit fullscreen mode

Why let over var?

The key difference is scopelet 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Must be initialised immediately

Unlike let, you cannot declare a const without giving it a value.

const country;        // ❌ SyntaxError
const country = "India"; // ✅ correct
Enter fullscreen mode Exit fullscreen mode

When to use let vs const?

A simple rule of thumb:

Use const by default. Switch to let only 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
Enter fullscreen mode Exit fullscreen mode

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!
Enter fullscreen mode Exit fullscreen mode

Number

JavaScript has just one number type — it handles both whole numbers and decimals.

let age = 25;
let price = 49.99;
let temperature = -5;
Enter fullscreen mode Exit fullscreen mode

You can also do math with numbers:

let total = 100 + 50;   // 150
let half = 100 / 2;     // 50
let squared = 4 ** 2;   // 16
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

BigInt only works with other BigInts — you can't mix them with regular numbers:

let a = 100n + 200n; // ✅ 300n
let b = 100n + 5;    // ❌ TypeError
Enter fullscreen mode Exit fullscreen mode

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!");
}
Enter fullscreen mode Exit fullscreen mode

Undefined

A variable that has been declared but not yet given a value is undefined.

let score;
console.log(score); // undefined
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

💡 undefined vs null: undefined means a value was never assigned. null means 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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)