Getting Started with Variables and Conditionals in JavaScript
Welcome back to AutistiCoder! Today, we’re diving into some fundamental coding concepts: variables and conditionals. These tools will help us manage data and make decisions in our code as we start building out the Pokémon app project. We’ll also explore how to run our code with Node.js. Let’s jump in!
Setting Up Your Project in VSCode
First, open your pokeapi folder in VSCode and navigate to the app directory. This is where we’ll be writing the code for this project.
Understanding Variables in JavaScript
In JavaScript, variables are like containers that store data we can access and modify as we go. For example:
let pokemonName = "Pikachu";
let baseHP = 35;
In this code, pokemonName stores our Pokémon’s name, while baseHP stores its health. These variables let us easily reference and manipulate Pokémon data in our program.
Using Conditionals for Decision-Making
Conditionals allow us to make decisions in our code based on whether something is true or false. For example, we can check if a Pokémon’s HP is above a certain level:
if (baseHP > 50) {
console.log(pokemonName + " has high base HP!");
} else {
console.log(pokemonName + " has low base HP.");
}
This code checks if baseHP is greater than 50. If it is, we see a message saying the Pokémon has high base HP; otherwise, it has low base HP.
Putting It All Together in pokemonData.js
To implement these concepts, add the following code to pokemonData.js:
let pokemonName = "Pikachu";
let baseHP = 35;
if (baseHP > 50) {
console.log(pokemonName + " has high base HP!");
} else {
console.log(pokemonName + " has low base HP.");
}
Running Your Code in Node.js
To test the code, open your terminal and navigate to your app folder:
cd Documents/pokeapi/app
Then, run the script:
node pokemonData.js
Expected Output
If successful, your terminal should display:
Pikachu has low base HP.
This output shows that our variables and conditionals are working as expected!
What’s Next?
With variables and conditionals under our belt, we’re ready to take our Pokémon app to the next level. In the next lesson, we’ll work with loops to handle lists of Pokémon data. Stay tuned for more coding fun!

Leave a Reply
You must be logged in to post a comment.