Data Types
Data Types - CS111 Review
| Back | Index | Next |
|---|---|---|
| Control Structures | Index | Operators |
Data Types
Click a type to see how it's used in Heist.exe.
Data Types in Heist.exe
Data types define what kind of information your code works with — and how it behaves. Every moving Guard, collected Gem, and music track in Heist.exe runs on a specific mix of these five types.
The Big Picture
mindmap
root((Data Types))
Number
n1["velocity.y = -3"]
n2["value: 5 per gem"]
n3["SCALE_FACTOR: 10"]
n4["INIT_POSITION<br>x & y"]
String
s1["src: heist-guard.png"]
s2["id: Guard1 / gem1"]
s3["iTunes search query"]
s4["Console log messages"]
Boolean
b1["permanentlyCollected"]
b2["playerDestroyed"]
b3["isPlaying / started"]
b4["response.ok"]
Array
a1["this.classes —<br>17 entries"]
a2["pool of iTunes tracks"]
a3["gameObjects list"]
JSONObject["JSON Object"]
j1["sprite_data_guard config"]
j2["gem_data config"]
j3["hitbox percentages"]
j4["keypress bindings"]
The Five Core Types
Number — any value measured, counted, or used in math
Exactly what it is: Numbers are used for anything involving quantity, position, velocity, or scale. JavaScript doesn’t distinguish between integers and decimals — both are the Number type. Numbers can be positive, negative, or zero.
From Heist.exe:
// Guard.js — lines 6-7
this.velocity.y = -3; // Guard's initial upward speed (negative = up)
// HeistL2.js — sprite configuration
SCALE_FACTOR: 10, // Each unit of sprite is 10 pixels on screen
INIT_POSITION: { x: 220, y: 300 }, // Exact pixel spawn coordinates
// Gem.js
value: 5, // Points per gem collected
// HeistL2.js — hitbox percentages
hitbox: { widthPercentage: 0.45, heightPercentage: 0.2 } // Collision box is 45% sprite width, 20% height
Numbers represent physics, positions, scales, and scores. If it’s measured in units or involved in math, it’s a Number.
String — text labels, file paths, and identifiers
Exactly what it is: Strings are sequences of characters wrapped in quotes (" or ' or backticks `). They’re used for anything that’s fundamentally text: file paths, object IDs, messages, and API URLs.
From Heist.exe:
// Guard.js & Gem.js — identify objects
id: 'Guard1'
id: 'gem'
// HeistL2.js — sprite image paths
src: path + "/images/projects/heist-exe/heist-guard.png"
src: path + "/images/projects/heist-exe/gem.png"
// Gem.js — console message (template literal)
`Gem collected! +${this.value} | Total: ${this.gameEnv?.stats?.coinsCollected}`
// heistMusic.js — iTunes Search API endpoint
`https://itunes.apple.com/search?term=${encodeURIComponent(picked)}&entity=song&limit=25`
Strings tell the engine what to load, what to call things, and where to find resources. They’re fundamentally text, even if they represent a file path or a number.
Boolean — on/off switches for game logic (only true or false)
Exactly what it is: A Boolean is a flag with exactly two possible values: true or false. Booleans control branching — do this or don’t. They track state: is the gem collected? Is the player destroyed? Is the music playing?
From Heist.exe:
// Guard.js — lines 7: collision gate
this.playerDestroyed = false; // Prevents collision from firing twice
// Gem.js — line 13: collection gate
this.permanentlyCollected = false; // Prevents collection from firing twice
// Gem.js — collect() method
if (this.permanentlyCollected) return; // Early exit if already collected
// Guard.js — update() method
if (!this.playerDestroyed && this.collisionChecks()) { ... } // Two booleans gated with &&
Booleans are the locks and switches of game logic. Almost every “do this only once” or “only if X is true” behavior relies on one.
Array — ordered list of related items
Exactly what it is: An Array is a numbered collection of values. Each value has an index (position) starting from 0. Arrays are used to group related data so you can loop through them, add items, or access specific items by position.
From Heist.exe:
// HeistL2.js — this.classes array: 17 entries to instantiate at startup
this.classes = [
{ class: GameEnvBackground, data: image_data_bg }, // Index 0
{ class: HeistPlayer, data: sprite_data_mc }, // Index 1
{ class: Gem, data: gem_data_1 }, // Index 2
{ class: Gem, data: gem_data_2 }, // Index 3
{ class: Guard, data: sprite_data_guard1 }, // Index 4
{ class: Guard, data: sprite_data_guard2 }, // Index 5
// ... 11 more entries
];
// Game engine loops this.classes:
this.classes.forEach(({ class: Class, data }) => {
new Class(data, gameEnv); // Loop runs once per array entry
});
// heistMusic.js — pool of valid iTunes tracks
let pool = []; // Start with empty array
data.results.forEach(track => {
if (!track.previewUrl) return;
if (track.artistName.match(VOCAL_HINTS)) return; // Skip vocal tracks
pool.push(track); // Add valid track to array
});
const chosen = pool[Math.floor(Math.random() * pool.length)]; // Pick random track
Arrays are used anywhere you have a collection of similar items: game objects to instantiate, music tracks to filter, items to loop through.
JSON Object — structured data with labeled properties
Exactly what it is: A JSON Object bundles related properties into a single package. Each property has a key (label) and a value (data). Objects let you group related data cleanly instead of creating separate variables.
From Heist.exe:
// HeistL2.js — sprite_data_guard1: complete configuration object
const sprite_data_guard1 = {
id: 'Guard1', // String property
name: 'guard1', // String property
src: path + "/images/projects/heist-exe/heist-guard.png", // String
SCALE_FACTOR: 10, // Number property
STEP_FACTOR: 500, // Number property
ANIMATION_RATE: 50, // Number property
INIT_POSITION: { x: 220, y: 300 }, // Nested object: x & y bundled
pixels: { height: 532, width: 400 }, // Nested object: image dimensions
hitbox: { widthPercentage: 0.45, heightPercentage: 0.2 }, // Nested object: collision ratio
keypress: { up: 87, left: 65, down: 83, right: 68 } // Nested object: key codes
};
// Gem.js — gemData: merged configuration
const gemData = {
id: 'gem',
value: 5,
SCALE_FACTOR: 10,
hitbox: { widthPercentage: 0.8, heightPercentage: 0.8 },
...data // Merge caller's customizations
};
// Access properties:
sprite_data_guard1.id // → 'Guard1'
sprite_data_guard1.INIT_POSITION.x // → 220 (nested access)
Objects let you bundle all spawn settings for a Guard into one variable, making constructors cleaner and data organization more intuitive.
Choosing the Right Type
| If you need to store… | Use |
|---|---|
| A speed, position, or score | Number |
| A file path, ID, or search term | String |
| A yes/no flag or state toggle | Boolean |
| A list of game objects or tracks | Array |
| A full entity configuration | JSON Object |
Common mistake: using a
Stringlike"true"instead of aBooleantrue. They look similar but behave completely differently in logic checks —if ("true")is always truthy, which will silently break your guard flags.