Chapter 5

Variables & Data Types

Learning Objectives

  • Understand variables and how to declare them
  • Learn different data types
  • Work with strings, numbers, and booleans
  • See how MixMind stores audio data

Declaring Variables

Variables are containers for storing data values. In modern JavaScript, we use let and const:

// let - for values that can change
let bpm = 128;
bpm = 130; // OK - we can change it

// const - for values that won't change
const sampleRate = 44100;
// sampleRate = 48000; // ERROR - can't reassign const

// var - old way (avoid using)
var oldWay = "don't use this";

Data Types

TypeExampleDescription
Number42, 3.14Integers and decimals
String"Hello"Text in quotes
Booleantrue, falseYes/No values
nullnullIntentionally empty
undefinedundefinedNot yet assigned
Array[1, 2, 3]Ordered list
Object{name: "Song"}Key-value pairs

Working with Data

// Numbers
let bpm = 128;
let duration = 240; // seconds
let energy = 0.85;

// Strings
let title = "Summer Nights";
let artist = 'DJ Mix';
let message = `BPM: ${bpm}`; // Template literal

// Booleans
let isPlaying = false;
let hasAnalysis = true;

// Arrays
let tracks = ["song1.mp3", "song2.mp3", "song3.mp3"];
console.log(tracks[0]); // "song1.mp3"

// Objects
let track = {
    title: "Summer Nights",
    bpm: 128,
    key: "Am",
    duration: 240
};
console.log(track.title); // "Summer Nights"

🎵 MixMind Connection

How MixMind Uses Variables

// Storing audio analysis results
const analysisResult = {
    bpm: 128,
    key: "Am",
    energy: 0.85,
    sections: [
        { start: 0, end: 30, type: "intro" },
        { start: 30, end: 90, type: "verse" }
    ]
};

// Storing track metadata
let currentTrack = {
    file: audioFile,
    buffer: audioBuffer,
    analysis: analysisResult
};

Exercise 5.1: Practice

Goal: Apply what you've learned in this chapter

Try the concepts from this chapter in your browser's console or a simple HTML file. Experiment with the code examples and modify them to see what happens.

📝 Quick Reference:

Review the key concepts from this chapter and make sure you understand each one before moving to the next chapter.