Variables and Data Types
What are variables?
A variable is a named container for a value. You store something in a variable so you can use it later, change it, or pass it to a function.
JavaScript has two modern ways to declare variables:
let and const.let vs const
const — use this by default. It means the variable can't be reassigned:JavaScript
const name = "Manish";
const pi = 3.14159;
name = "Someone"; // Error! Can't reassign a constlet — use this when you need to reassign the value:JavaScript
let score = 0;
score = score + 10; // Works fine
score += 5; // Shorthand — score is now 15What about
var? You'll see it in older code, but avoid it. var has confusing scoping behavior that let and const fix. Always use const first; switch to let only if you need to reassign.Data types
JavaScript has several built-in types:
Strings — text wrapped in quotes:
JavaScript
const greeting = "Hello, world!";
const name = 'Manish';
const message = `Welcome, ${name}!`; // Template literal (backticks)Template literals (backticks) let you embed expressions with `$
` — extremely useful.
Numbers — integers and decimals are the same type:
Numbers — integers and decimals are the same type:
JavaScript
const age = 25;
const price = 9.99;
const total = price * 3; // 29.97Booleans —
true or false:JavaScript
const isLoggedIn = true;
const hasPermission = false;null and undefined:
JavaScript
let user = null; // Explicitly "no value"
let address; // undefined — declared but not assignedArrays — ordered lists:
JavaScript
const colors = ["red", "green", "blue"];
console.log(colors[0]); // "red"
console.log(colors.length); // 3Objects — key-value pairs:
JavaScript
const person = {
name: "Manish",
age: 25,
isStudent: true,
};
console.log(person.name); // "Manish"Naming conventions
- Use camelCase for variables:
firstName,isLoggedIn,totalPrice. - Make names descriptive:
userCountis better thanx. - Constants that never change are sometimes written in UPPERSNAKECASE:
const MAX_RETRIES = 3.
Common mistakes
Using
== instead of ===:JavaScript
console.log(2 == "2"); // true (loose equality — converts types!)
console.log(2 === "2"); // false (strict equality — compares type too)Always use
=== (strict equality). The loose == operator has surprising type coercion rules that lead to bugs.typeof and type checking
JavaScript
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" (historical quirk — use === null instead)
console.log(typeof {}); // "object"
console.log(typeof []); // "object" (arrays are objects)
console.log(Array.isArray([])); // true — proper array checkFor primitives,
typeof is enough. For null, use strict equality. For arrays, use Array.isArray().Mutability: const with objects and arrays
const prevents reassignment of the binding, not mutation of the value:JavaScript
const user = { name: "Alex" };
user.name = "Sam"; // OK — object contents changed
user = { name: "Bo" }; // Error — can't reassign user
const items = [1, 2, 3];
items.push(4); // OKIf you need an immutable snapshot, create a copy (`
{
...user }
, [
...items]
) before changing — covered in later lessons on modern JavaScript.
Truthy and falsy values
These values are falsy: false, 0, "", null, undefined, and NaN. Everything else is truthy, including "0" and empty arrays JSON
[]`.
JavaScript
if (userInput) {
// runs when userInput is not "", 0, null, undefined, false, or NaN
}Use explicit checks when zero or empty string are valid values (
userInput !== "", count !== null).Type coercion and comparisons
JavaScript sometimes converts types automatically when you use operators like
+ or loose equality. Concatenating a string and a number coerces the number to a string: "Score: " + 10 becomes "Score: 10". That convenience is also a common source of bugs. Stick to === and !== for comparisons, and use Number(), String(), or Boolean() when you need an explicit conversion.When you read values from HTML forms or APIs, they often arrive as strings even if they look numeric. Convert deliberately before doing math:
const quantity = Number(input.value) and check for NaN if the field is optional.Choosing the right container
Use a string for text, a number for quantities and measurements, and a boolean for yes/no flags. Choose an array when order matters — a list of comments, cart items, or steps in a wizard. Choose an object when you need named fields that belong together — a user profile, product details, or configuration options. You will often combine them: an array of user objects is one of the most common shapes in real applications.
Key takeaway
Start with
const for everything. Switch to let only when you need to reassign. Learn the basic types — strings, numbers, booleans, arrays, and objects — and you'll have the foundation for everything else in JavaScript.