Loading ...

user Hausa Academy
7th Mar, 2025 12:48:58
JavaScript Programming

JavaScript Hausa #10: Working with Numbers

Hello! Welcome back to our JavaScript lessons! Today, we’re diving into something super important: **numbers**. Numbers are the building blocks for calculations, scores, prices, and more in coding. Whether you want to add two values, calculate your budget, or even make a game, numbers are your best friend. Let’s explore how to use them in JavaScript!


What Are Numbers in JavaScript?

Numbers are values that represent quantities or measurements. In JavaScript, they can be whole numbers (like 5 or -10) or decimals (like 3.14 or 0.5). They’re used for math operations, counting, and even storing things like prices, scores, or temperatures.

Here’s how you create a number:

let age = 25;          // A whole number (integer)
let price = 9.99;      // A decimal number (float)
let temperature = -5;  // Negative numbers are okay too!


How to Use Numbers in JavaScript

1. Basic Math Operations

You can add, subtract, multiply, and divide just like in math class:

let sum = 10 + 5;       // 15
let difference = 20 - 3; // 17
let product = 4 * 6;    // 24
let division = 100 / 2; // 50


2. Combining Numbers with Strings

Sometimes, you’ll want to mix numbers and text to create messages. Use the `+` operator to glue them together:

let score = 100;
let message = "Your score is: " + score; // "Your score is: 100"


3. Rounding Numbers

JavaScript lets you round numbers to make them cleaner:

let decimal = 3.14159;
console.log(Math.round(decimal)); // 3
console.log(Math.ceil(decimal));  // 4 (rounds up)
console.log(Math.floor(decimal)); // 3 (rounds down)


4. Random Numbers

Create random numbers for games or quizzes:

let randomNum = Math.random(); // Gives a number between 0 and 1
console.log(randomNum * 10);   // Now between 0 and 10!


Real-Life Examples of Numbers

  • Calculators: Every math app uses numbers to add, subtract, and more.
  • E-commerce: Prices on websites are stored as numbers to calculate totals.
  • Gaming: Scores, health bars, and levels all rely on numbers.
  • Weather Apps: Temperatures and wind speeds are displayed as numbers.


Common Mistakes to Avoid

  1. Forgetting Decimals: If you write let price = 999, it’s a whole number. Add a decimal like 999.00 if you need cents.
  2. Mixing with Strings: "10" + 5 becomes "105" (text), but 10 + 5 is 15 (number). Always check your quotes!
  3. Dividing by Zero: 10 / 0 will break your code. Always check if the denominator is zero first.


Why Are Numbers Important?

Numbers power almost everything in coding. Without them:

  • You couldn’t calculate a user’s age or budget.
  • Games wouldn’t track scores or health.
  • Websites couldn’t show prices or shipping costs.


Watch This Video for More Practice

Still want to see numbers in action? Here’s a video in Hausa that explains how to work with numbers step-by-step. It’s perfect for visual learners:

Try these exercises after watching the video:

  1. Calculate your age in months (e.g., 25 years × 12 months).
  2. Round your favorite decimal number to the nearest whole number.
  3. Create a random number between 1 and 100 and print it.


Final Thoughts

Congratulations! You now know how to work with numbers in JavaScript. They might seem simple, but they’re the foundation of almost every app and website. The more you practice, the more creative you’ll get—like building a calculator or a dice-rolling game!

Remember: Coding is like learning a new language. Start with small projects and build up. Don’t worry if you make mistakes—every expert was once a beginner!

In the next lesson, we’ll explore more exciting topics. Keep coding, stay curious, and most importantly… have fun!

Thanks for reading, and happy coding!


Share This Article

Comments

Leave a comment

Blog categories