You are currently viewing JavaScript Cheat Sheet 10.17 : JavaScript Numbers

JavaScript Cheat Sheet 10.17 : JavaScript Numbers

Mastering JavaScript Numbers


Welcome to the exciting world of JavaScript programming! In this tutorial, we’ll delve into the fundamentals of working with numbers in JavaScript, focusing specifically on JavaScript String Methods. Whether you’re a beginner or looking to refresh your knowledge, this guide is designed to help you understand the basics and use JavaScript’s powerful features for handling numbers.

Section 1: Understanding JavaScript Numbers

JavaScript treats numbers as a primitive data type. You can perform various operations on numbers using built-in methods. Let’s explore some key concepts.

1.1 Basic Arithmetic Operations:

let num1 = 10;
let num2 = 5;

// Addition
let sum = num1 + num2;
console.log("Sum:", sum);

// Subtraction
let difference = num1 - num2;
console.log("Difference:", difference);

// Multiplication
let product = num1 * num2;
console.log("Product:", product);

// Division
let quotient = num1 / num2;
console.log("Quotient:", quotient);

Explanation:

  • Declare two variables (num1 and num2) with numeric values.
  • Perform basic arithmetic operations (addition, subtraction, multiplication, division).
  • Display the results using console.log().

1.2 Using Modulus Operator:

let dividend = 10;
let divisor = 3;

let remainder = dividend % divisor;
console.log("Remainder:", remainder);

Explanation:

  • Introduce the modulus operator %.
  • Calculate the remainder of the division.

Section 2: JavaScript String Methods for Numbers

JavaScript provides powerful string methods that can be applied to numbers when they are represented as strings.

2.1 Converting Numbers to Strings:

let number = 42;

// Using toString() method
let strNumber = number.toString();
console.log("Number as String:", strNumber);

// Using String() function
let strNumberAlt = String(number);
console.log("Number as String (alternative):", strNumberAlt);

Explanation:

  • Convert a numeric variable to a string using toString() method and String() function.

2.2 Extracting Specific Digits:

let largeNumber = 98765;

// Extracting the first two digits
let firstTwoDigits = String(largeNumber).slice(0, 2);
console.log("First Two Digits:", firstTwoDigits);

// Extracting the last three digits
let lastThreeDigits = String(largeNumber).slice(-3);
console.log("Last Three Digits:", lastThreeDigits);

Explanation:

  • Convert a large number to a string.
  • Use the slice() method to extract specific digits.

2.3 Checking if a String Contains a Number:

let text = "The answer is 42";

// Using includes() method
let containsNumber = text.includes("42");
console.log("Contains Number:", containsNumber);

Explanation:

  • Use the includes() method to check if a string contains a specific number.

Conclusion


Congratulations! You’ve successfully explored the basics of working with numbers in JavaScript using String methods. These skills form a solid foundation for more advanced JavaScript programming. Continue practicing and experimenting to strengthen your understanding.

Remember, mastering JavaScript numbers opens the door to countless possibilities in web development and beyond.

Leave a Reply