JavaScript — Lesson 2
Variables & Data Types
Storing data with variables and understanding types.
Variables & Data Types
Variables store data values. JavaScript has several data types and three ways to declare variables.
Declaring Variables
let name = "Alice"; // Can be reassigned
const age = 25; // Cannot be reassigned (preferred)
var oldWay = "avoid"; // Old way, avoid using
When to Use let vs const
- Use const by default - for values that won't change
- Use let when you need to reassign the variable
- Avoid var - it has confusing scoping rules
Data Types
// String: text
const greeting = "Hello";
// Number: integer or decimal
const count = 42;
const pi = 3.14;
// Boolean: true or false
const isActive = true;
// Array: list of values
const fruits = ["apple", "banana", "orange"];
// Object: key-value pairs
const person = {
name: "Alice",
age: 25,
city: "New York"
};
// null: intentional empty value
const empty = null;
// undefined: variable declared but not assigned
let notDefined;
Checking Types
typeof "Hello"; // "string"
typeof 42; // "number"
typeof true; // "boolean"
typeof []; // "object" (arrays are objects)
Naming Conventions
- Use camelCase:
firstName,totalPrice - Start with a letter, underscore, or $
- Use descriptive names:
userAgenotua
Tip: Use descriptive variable names. Your future self will thank you when you come back to the code months later!
Practice what you learned in the Deoit Editor — a free, browser-based code editor.
Open Editor →