Data Types Showcase
Data Types
Table of Contents
Data types are the building blocks of all programs. You use numbers, strings, booleans, arrays, and objects to store and manipulate information.
Subrequirements
Numbers
- Pong Game: Ball position, velocity, and score (View Pong Game)
- Calculator: Arithmetic operations (View Calculator)
// From hacks/pong.md — Config object stores all numeric game values
const Config = {
canvas: { width: 800, height: 500 },
paddle: { width: 10, height: 100, speed: 10.5 },
ball: { radius: 10, baseSpeedX: 5, maxSpeed: 15 },
rules: { winningScore: 11 }
};
// Ball position and velocity are numbers updated every frame
// From hacks/pong.md — Ball.update()
this.position.x += this.velocity.x;
this.position.y += this.velocity.y;
Code Runner Challenge
Hit Run to see numeric properties in action — position, velocity, and score tracking exactly as used in Pong. Change the speed value and re-run to see how it affects the ball movement simulation.
Quick Quiz: Numbers
- What data type is used to store a player’s score in Pong?
- a) String
- b) Number
- c) Boolean
- What happens if you forget to use parseInt() when reading a number from localStorage?
- How does changing the ball speed value affect the game?
Show Answers
1. b) Number 2. You might get string concatenation instead of addition, causing bugs. 3. The ball moves faster or slower, changing the difficulty.Strings
- Pong Game: Displaying scores and messages
- Homeworks: Strings Homework
// From _notebooks/JavaScriptLessons/Strings/2026-01-27-Strings.ipynb
let firstName = "John";
let lastName = "Smith";
let fullName = firstName + " " + lastName;
console.log(fullName); // "John Smith"
// From hacks/pong.md — Renderer.text() draws string score/messages on canvas
text(t, x, y, color = Config.visuals.text) {
this.ctx.fillStyle = color;
this.ctx.font = "30px Arial";
this.ctx.fillText(t, x, y);
}
// Called as: this.renderer.text("Speed: " + maxSpeed.toFixed(2), canvas.width - 200, 30);
Code Runner Challenge
Hit Run to see string manipulation — concatenation, template literals, and methods — exactly as used in Pong's score display and the Strings homework. Try changing the playerName value.
Booleans
- Pong Game: Flags for pause, win, and collision (View Pong Game)
- Homeworks: Booleans Homework
// From hacks/pong.md — Boolean state flags in the Game class
this.gameOver = false;
this.paused = false;
// togglePause flips the boolean
togglePause() { this.paused = !this.paused; if (!this.paused) this.loop(); }
// From _notebooks/JavaScriptLessons/Booleans — boolean operators in game-like logic
let hasLicense = true;
let hasCar = false;
let canDrive = hasLicense && hasCar; // false — both must be true
console.log("Can drive: " + canDrive);
Code Runner Challenge
Hit Run to see boolean flags controlling game state — the same pattern as Pong's gameOver and paused flags. Watch how toggling one boolean changes which code path runs.
Arrays
- Pong Game: Array of active balls that grows during multi-ball power-up (View Pong Game)
- Homeworks: Arrays Homework
// From hacks/pong.md — Game stores multiple balls in an array
this.balls = [ new Ball(Config.ball.radius, Config.canvas.width, Config.canvas.height) ];
// Loop through all balls to update them (from Game.update())
for (const b of this.balls) { b.update(); }
// From _notebooks/JavaScriptLessons/Arrays — modifying arrays
let colors = ["red", "blue", "green"];
colors.push("purple"); // add element
colors.splice(colors.indexOf("red"), 1); // remove element
console.log(colors); // ["blue", "green", "purple"]
Code Runner Challenge
Hit Run to see array operations — push, splice, forEach, and for..of — exactly as used in Pong's multi-ball system and the Arrays homework. Try changing the starting ball count.
Objects (JSON)
- Pong Game: Full Config object with nested objects for every game setting (View Pong Game)
- Homeworks: JSON Homework
// From hacks/pong.md — nested Config object organizes all game settings
const Config = {
canvas: { width: 800, height: 500 },
paddle: { width: 10, height: 100, speed: 10.5 },
ball: { radius: 10, baseSpeedX: 5, maxSpeed: 15 },
powerUp: { spawnMinSec: 6, spawnMaxSec: 14, durationSec: 6 },
visuals: { bg: "#000", fg: "#fff", text: "#fff", win: "yellow" }
};
// From _notebooks/JavaScriptLessons/JSON — JavaScript object with function property
const student = {
name: "Alex",
age: 16,
enrolled: true,
greet: function () { return "Hello!"; }
};
console.log(student.name); // "Alex"
Code Runner Challenge
Hit Run to see nested objects, property access, and JSON serialization — the same structure as Pong's Config object and the leaderboard sprite data in localStorageDemo.js.
Gamerunner — Confetti Cannon
Challenge
Confetti cannon — array of particle objects, auto-fires on load and on every click!