Building a Wildlife Tracker with JavaScript Objects 🐅

2026-11-18来源 dev.toEnglish
Building a Wildlife Tracker with JavaScript Objects 🐅

Learning to code is a continuous journey, and I believe one of the best ways to strengthen programming concepts is to put them into practice.

Today, I built a small JavaScript project called Wildlife Tracker as part of my ongoing learning journey.

The goal was to practise working with JavaScript objects and understand how their properties can be accessed, added, updated, and deleted.

Let's explore what I practised!

  1. Creating JavaScript Objects

I started by creating two objects representing different animals: a tiger and an elephant.

Each object contains three properties:

species: Represents the animal's name.

age: Stores the animal's age.

isEndangered: Indicates whether the animal is endangered.

const tiger = {
species: "Tiger",
age: 5,
isEndangered: true
};

const elephant = {
species: "Elephant",
age: 10,
isEndangered: true
};

This helped me practise organizing related information using objects.

  1. Accessing Object Properties

I created functions to retrieve an animal's species and age using dot notation.

const getSpecies = (animal) => {
return animal.species;
};

const getAge = (animal) => {
return animal.age;
};

console.log(getSpecies(tiger));
console.log(getAge(tiger));

Dot notation provides a straightforward way to access a specific property of an object.

  1. Adding New Properties

Next, I practised adding a new property to an existing object.

For this, I created an addHabitat() function.

const addHabitat = (animal, habitat) => {
animal.habitat = habitat;
return animal;
};

console.log(addHabitat(tiger, "Rainforest"));

This adds a new habitat property to the tiger object.

  1. Updating Existing Properties

Objects also allow us to modify existing property values.

I created an updateAge() function to update an animal's age.

const updateAge = (animal, newAge) => {
animal.age = newAge;
return animal;
};

console.log(updateAge(elephant, 12));

Here, the elephant's age changes from 10 to 12.

  1. Deleting Object Properties

I also explored the JavaScript delete operator.

It allows us to remove a property from an object.

const removeEndangeredStatus = (animal) => {
delete animal.isEndangered;
return animal;
};

console.log(removeEndangeredStatus(tiger));

This removes the isEndangered property from the tiger object.

  1. Checking Whether a Property Exists

How can we check whether an object contains a particular property?

I used the hasOwnProperty() method for this.

const hasHabitat = (animal) => {
return animal.hasOwnProperty("habitat");
};

console.log(hasHabitat(tiger)); // true
console.log(hasHabitat(elephant)); // false

The method returns true if the object has the specified own property; otherwise, it returns false.

  1. Accessing Properties Using Bracket Notation

Finally, I practised accessing object properties dynamically using bracket notation.

function getProperty(animal, propertyName) {
return animal[propertyName];
}

console.log(getProperty(tiger, "species"));
console.log(getProperty(elephant, "age"));

Unlike dot notation, bracket notation allows us to use a variable to determine which property we want to access.

This is particularly useful when the property name is not known until runtime.

What I Learned

Through this small project, I practised several fundamental JavaScript object operations:

Creating objects.

Accessing properties using dot notation.

Accessing properties using bracket notation.

Adding new properties.

Updating existing properties.

Deleting properties.

Checking whether a property exists.

Although this was a small project, it gave me an opportunity to apply these concepts in code rather than just reading about them.

My takeaway: Understanding a concept is important, but applying it through practice helps build a stronger foundation.

I'm continuing to learn, experiment, and build. There's always something new to explore! 🚀

BuildInPublic #JavaScript #WebDevelopment #LearningToCode #Beginners

原文:https://www.a8q9.cn/en/article/building-a-wildlife-tracker-with-javascript-objects-afeb78.html

相关文章