JavaScript Variables and Data Types

Beginner
70 XP
25 min
Lesson Content

JavaScript Variables and Data Types

Variables in JavaScript are containers for storing data values. You can declare variables using let, const, or var.

Variable Declaration

let name = 'John';        // String
const age = 25;           // Number
let isStudent = true;     // Boolean
let hobbies = ['reading', 'coding'];  // Array
let person = { name: 'Alice', age: 30 };  // Object

Data Types

  • String: Text data enclosed in quotes ('text' or "text")
  • Number: Numeric values (integers and decimals)
  • Boolean: true or false values
  • Array: Ordered list of values [1, 2, 3]
  • Object: Collection of key-value pairs {key: value}
  • undefined: Variable declared but not assigned
  • null: Intentionally empty value

Best Practices

  • Use const for values that won't change
  • Use let for values that will change
  • Avoid var in modern JavaScript
  • Use descriptive variable names (camelCase)
Example Code

Declare and use different types of variables

// Declare variables with different data types
let studentName = 'Alex';
const currentYear = 2024;
let isEnrolled = true;
let grades = [85, 92, 78, 96];

// Display the variables
console.log('Student Name:', studentName);
console.log('Current Year:', currentYear);
console.log('Is Enrolled:', isEnrolled);
console.log('Grades:', grades);

Expected Output:

Student Name: Alex
Current Year: 2024
Is Enrolled: true
Grades: [85,92,78,96]
Study Tips
  • Read the theory content thoroughly before practicing
  • Review the example code to understand key concepts
  • Proceed to the Practice tab when you're ready to code