How Tamagotchi's can teach you JavaScript Functions, Objects and Methods
Build the basics of a tiny digital pet while learning how JavaScript functions, objects and methods work together using a Tamagotchi-style example.
Functions, objects, and methods
For some, JavaScript can feel abstract until you attach it to something real. A Tamagotchi-style pet is perfect because it has state (health, enjoyment) and behavior (eat, play). That maps cleanly to:
- Functions: reusable blocks of logic.
- Objects: collections of related data.
- Methods: functions that live on an object and act on its data.
Let’s build a tiny pet and use it to explain each concept step by step contextually.
Functions: reusable logic
A function is a named block of code you can run whenever you need it. We don't want to copy and paste the same code block over and over while only changing one or more values; so instead we can re-run the same function with different parameters.
Codefunction clamp(value, min, max) { return Math.min(Math.max(value, min), max); }
This clamp function keeps a number within a given range. We’ll use it to make sure our pet’s stats don’t go below 0 or above 100 by checking the value against a min of 0 and max of 100.
Objects: storing state
An object groups data that belongs together. Our Tamagotchi has a name, health, and enjoyment.
Codeconst tamagotchi = { name: 'Mochi', health: 70, enjoyment: 55, };
Now we have a single structure that represents the pet’s current state.
Methods: behaviour that uses object data
A method is just a function stored on an object. Methods are great when behaviour belongs to the data itself.
Let’s give our pet an eat method.
Codeconst tamagotchi = { name: 'Mochi', health: 70, enjoyment: 55, eat(food) { if (food === 'fruit') { this.health += 15; this.enjoyment += 2; } if (food === 'chocolate') { this.health -= 5; this.enjoyment += 10; } this.health = clamp(this.health, 0, 100); this.enjoyment = clamp(this.enjoyment, 0, 100); }, };
Which equates to:
- The method uses this to access the object’s data (this.health and this.enjoyment).
- We call clamp to keep values realistic.
Add another method: play time
Now we can add a method that boosts enjoyment and slightly tires the pet out:
Codeconst tamagotchi = { name: 'Mochi', health: 70, enjoyment: 55, eat(food) { if (food === 'fruit') { this.health += 15; this.enjoyment += 2; } if (food === 'chocolate') { this.health -= 5; this.enjoyment += 10; } this.health = clamp(this.health, 0, 100); this.enjoyment = clamp(this.enjoyment, 0, 100); }, play(activity) { if (activity === 'fetch') { this.enjoyment += 15; this.health -= 2; } if (activity === 'nap') { this.enjoyment += 5; this.health += 5; } this.health = clamp(this.health, 0, 100); this.enjoyment = clamp(this.enjoyment, 0, 100); }, };
Putting it all together
Here’s how you would use these methods in practice:
Codetamagotchi.eat('fruit'); console.log(tamagotchi.health); // 85 console.log(tamagotchi.enjoyment); // 57 tamagotchi.eat('chocolate'); console.log(tamagotchi.health); // 80 console.log(tamagotchi.enjoyment); // 67 tamagotchi.play('fetch'); console.log(tamagotchi.health); // 78 console.log(tamagotchi.enjoyment); // 82
What just happened?
- eat and play are methods because they live on the object.
- clamp is a function because it is standalone, reusable logic.
- tamagotchi is an object that stores data and behavior together.
Why this matters
When your code grows, you will want to organise it around things: a pet, a user profile, a shopping cart, a playlist. Each thing has:
- Data (object properties)
- Behavior (methods)
- Shared utilities (regular functions)
That mental model eventually scales from a tiny Tamagotchi to full scale apps.
Final takeaway
To sum them up with as few words as possible:
- Functions are reusable recipes.
- Objects keep related data together.
- Methods are functions that belong to an object and operate on its data.
Now to go feed my digital pet (preferably fruit).