Variables & Data Types
let, const, string, number, boolean
Skills taught: js.variables, js.data-types, js.typeof
Declaring variables
// const — cannot be reassigned (use by default)
const name = 'KERNAL';
const year = 2026;
const isOnline = true;
// let — can be reassigned
let score = 0;
score = score + 10; // OK
score += 10; // shorthand
// var — AVOID in modern code
var old = 'do not use'; // has confusing function scope and hoisting
// Naming conventions
const firstName = 'Prashant'; // camelCase (standard JS)
const MAX_SIZE = 100; // UPPER_SNAKE_CASE for constants
const UserProfile = {}; // PascalCase for classes/constructors
The 7 primitive types
// String — text in single, double, or backtick quotes
const greeting = 'Hello';
const html = "<h1>Title</h1>";
const template = `Hello, ${name}!`; // template literal
// Number — integers and decimals (one type for both)
const age = 25;
const pi = 3.14159;
const big = 1_000_000; // underscores for readability
// Boolean — true or false only
const isDone = false;
const isReady = true;
// null — intentionally empty (you set this)
const user = null;
// undefined — declared but not assigned (JS sets this)
let unset;
console.log(unset); // undefined
// Object — key-value pairs (covered in a later lesson)
const person = { name: 'Prashant', age: 27 };
// Array — ordered list (technically an object)
const techs = ['HTML', 'CSS', 'JavaScript'];
Checking types with typeof
typeof 'hello' // 'string'
typeof 42 // 'number'
typeof true // 'boolean'
typeof undefined // 'undefined'
typeof null // 'object' ← famous JS bug (null is NOT an object)
typeof {} // 'object'
typeof [] // 'object' ← arrays are objects
typeof function(){} // 'function'
Warning: typeof null returns 'object' — this is a long-standing JavaScript bug that cannot be fixed for backwards compatibility. To check for null: use value === null.
Type coercion — automatic conversion
// JavaScript automatically converts types in some operations
'5' + 3 // '53' (number 3 converted to string)
'5' - 3 // 2 (string '5' converted to number)
'5' * '2' // 10 (both converted to numbers)
Boolean(0) // false
Boolean('') // false
Boolean(null) // false
Boolean([]) // true (empty array is truthy!)
// Use === (strict equality) to avoid coercion surprises
5 == '5' // true (loose — converts types)
5 === '5' // false (strict — no conversion) ← always use this
Tip: Always use === and !== for comparisons. The loose equality operators (== and !=) perform type coercion that leads to surprising results.
Frequently Asked Questions
What is the difference between undefined and null?
undefined means a variable was declared but never assigned a value — JavaScript sets this automatically. null means intentionally absent — you set this deliberately to indicate 'no value here'. Both are falsy.
Can I change the type of a variable?
Yes. JavaScript is dynamically typed — you can assign a string to a variable and later assign a number to the same variable. This is flexible but can lead to bugs; TypeScript adds static types to prevent this.
Why is 0.1 + 0.2 not exactly 0.3 in JavaScript?
JavaScript uses IEEE 754 floating-point arithmetic, which cannot represent all decimal fractions exactly in binary. 0.1 + 0.2 = 0.30000000000000004. For money, use integer cents (store 150 for £1.50) or a decimal library.