Data Types
Table of Contents
Numbers
In coding, numbers are data types used to perform mathematical operations, track game scores, or define coordinates on a screen. They are generally categorized into integers (whole numbers) and floats (numbers with decimals), each requiring different amounts of memory.
class Player extends Character {
constructor(data, gameEnv) {
super(data, gameEnv);
// Position — where the player is on the canvas
this.x = 100;
this.y = 300;
// Velocity — how many pixels to move each frame
this.velocityX = 0;
this.velocityY = 0;
// Health and score tracking
this.health = 100;
this.score = 0;
this.speed = data.speed || 4;
}
update() {
// All arithmetic on numeric properties each frame
this.velocityY += 0.4; // gravity
this.x += this.velocityX;
this.y += this.velocityY;
this.score += 1; // increment each frame survived
}
}
In this code, numbers are the engine that drives both the physics and the logic. Every movement you see on the screen is actually just a mathematical calculation being updated 60 times per second.
Strings
A string in coding is a data type used to represent text. It is essentially a sequence or “string” of characters—including letters, numbers, symbols, and spaces—all grouped together as a single unit.
class NPC extends Character {
constructor(data, gameEnv) {
super(data, gameEnv);
// Character name — string used in HUD and dialogue
this.name = data.name || 'NPC';
// Sprite path — string fed to Image() loader
this.spriteSrc = `./images/sprites/${this.name}/idle.png`;
// Game state — controls which animation branch runs
this.state = 'idle'; // 'idle' | 'walk' | 'talk' | 'hurt'
}
setState(newState) {
this.state = newState;
this.spriteSrc = `./images/sprites/${this.name}/${newState}.png`;
}
}
This code uses strings as dynamic identifiers and file system paths. By combining fixed text with variable data, strings allow the game engine to dynamically figure out which image to load and which text to display for different characters.
Boolean
boolean is a fundamental data type in programming that can hold only one of two possible values: True or False
class Player extends Character {
constructor(data, gameEnv) {
super(data, gameEnv);
// Movement flags
this.isJumping = false;
this.isMovingLeft = false;
this.isMovingRight = false;
// State flags
this.isPaused = false;
this.isVulnerable = true;
this.isShielded = false;
}
jump() {
// Boolean guards the action — can't jump mid-air
if (!this.isJumping) {
this.velocityY = -8;
this.isJumping = true; // flip the flag
}
}
}
In this code, Booleans serve as the decision-making switches that govern the player’s physical capabilities and state tracking. By initializing properties like this.isJumping as binary flags, the engine can create strict logic gates, or “guards,” that prevent illegal actions like infinite mid-air jumping.
Arrays
An array is a fundamental data structure in programming that stores a collection of values under a single name. It allows you to organize multiple related items—like a list of scores or a set of names—without creating a separate variable for every single piece of data.
// Array of active game objects — iterated every frame
const gameObjects = [player, npc, enemy1, enemy2, platform1];
// Game loop updates every object using forEach
function gameLoop() {
gameObjects.forEach(obj => obj.update());
gameObjects.forEach(obj => obj.draw());
}
// Level data stored as array of config objects
const levelData = [
{ type: 'platform', x: 0, y: 400, width: 200 },
{ type: 'platform', x: 300, y: 320, width: 150 },
{ type: 'enemy', x: 500, y: 280, speed: 2 },
];
// Add / remove objects dynamically
gameObjects.push(new Enemy(enemyData, gameEnv));
gameObjects.filter(obj => obj.health > 0); // remove dead
// Animation frames stored as array of sprite coordinates
this.frames = [
{ sx: 0, sy: 0 },
{ sx: 64, sy: 0 },
{ sx: 128, sy: 0 },
n this code, arrays serve as centralized, dynamic lists that group related game objects, level blueprints, and animation frames together for easy management. By using collection methods like .forEach() and .filter(), the game can update the entire world at once and easily add or remove characters as the game changes. This eliminates the need to track dozens of individual variables, keeping the engine’s memory structured and organized.
Objects(JSON)
In JavaScript, objects are the building blocks of the language. They are collections of related data (properties) and behavior (methods) stored as key-value pairs.
// Character config object — passed as 'data' to constructor
const playerData = {
name: 'mario',
x: 100,
y: 300,
width: 48,
height: 48,
speed: 4,
health: 100,
sprite: './images/sprites/mario/idle.png',
};
// GameLevel setup object — instantiates all game objects
const GameSetup = {
player: { data: playerData, class: Player },
enemies: [
{ data: { name: 'goomba', x: 400, speed: 2 }, class: Enemy },
],
platforms: [
{ data: { x: 0, y: 400, width: 800 }, class: Platform },
],
};
// Access properties with dot notation
const name = playerData.name; // 'mario'
const startX = playerData.x; // 100
// Destructuring — pull multiple properties at once
const { x, y, width, height } = playerData;
Objects let you group related data under one variable — instead of separate playerX, playerY, playerHealth variables, everything lives in one playerData object with named keys. You access individual values with dot notation like playerData.name or pull several out at once with destructuring.