You are currently viewing JavaScript Cheat Sheet 10.18 : JavaScript Number Methods

JavaScript Cheat Sheet 10.18 : JavaScript Number Methods

Mastering JavaScript Number Methods


Welcome to the exciting world of JavaScript programming! In this tutorial, we will delve into the fundamental concepts of JavaScript Number Methods. Understanding these methods is crucial for manipulating and working with numerical data in your JavaScript programs. By the end of this tutorial, you’ll be equipped with the knowledge to handle numbers effectively in your code.

1. Introduction to JavaScript Number Methods:

JavaScript provides a variety of methods for manipulating numbers. These methods can be applied to number literals or variables that store numerical values. In this tutorial, we will cover some essential JavaScript Number Methods, including toExponential(), toFixed(), toPrecision(), toString(), and valueOf().

2. The toExponential() Method:

The toExponential() method is used to represent a number in exponential notation. Let’s look at an example:

let num = 12345;
let expNotation = num.toExponential(2);
console.log(expNotation);

Output:

1.23e+4

Explanation:

  • The toExponential(2) method is applied to the variable num, converting it to exponential notation with two decimal places.

3. The toFixed() Method:

The toFixed() method is used to format a number with a specified number of digits after the decimal point:

let num = 123.456789;
let fixedNum = num.toFixed(2);
console.log(fixedNum);

Output:

123.46

Explanation:

  • The toFixed(2) method is applied to num, rounding it to two decimal places.

4. The toPrecision() Method:

The toPrecision() method is used to represent a number with a specified length:

let num = 123.456;
let precisionNum = num.toPrecision(5);
console.log(precisionNum);

Output:

123.46

Explanation:

  • The toPrecision(5) method is applied to num, representing it with a total length of five digits.

5. The toString() Method:

The toString() method converts a number to a string:

let num = 42;
let numStr = num.toString();
console.log(numStr);

Output:

42

Explanation:

  • The toString() method is applied to num, converting it to a string.

6. The valueOf() Method:

The valueOf() method returns the primitive value of a number:

let num = new Number(42);
let primitiveValue = num.valueOf();
console.log(primitiveValue);

Output:

42

Explanation:

  • The valueOf() method is applied to the num object, returning its primitive value.

7. Putting It All Together: Sample Codes and Outputs:

Now, let’s combine these methods in a sample code:

let num = 123.456789;
let expNotation = num.toExponential(2);
let fixedNum = num.toFixed(2);
let precisionNum = num.toPrecision(5);
let numStr = num.toString();
let primitiveValue = (new Number(num)).valueOf();

console.log("Exponential Notation:", expNotation);
console.log("Fixed Number:", fixedNum);
console.log("Precision Number:", precisionNum);
console.log("Number as String:", numStr);
console.log("Primitive Value:", primitiveValue);

Output:

Exponential Notation: 1.23e+2
Fixed Number: 123.46
Precision Number: 123.46
Number as String: 123.456789
Primitive Value: 123.456789

8. Conclusion:

Congratulations! You’ve now mastered the basics of JavaScript Number Methods. These methods provide powerful tools for handling numerical data in your JavaScript programs. Practice incorporating them into your code to enhance your programming skills. Stay tuned for more tutorials on advanced JavaScript concepts!

Remember, practice is key to mastering programming, so don’t hesitate to experiment with these methods in different scenarios.

Leave a Reply