Skip to content

Buzzingz

Unleashing the Power of Code

  • HTML
  • CSS
  • JavaScript
  • Toggle search form

JavaScript Number Methods

Posted on April 2, 2023April 2, 2023 By shani No Comments on JavaScript Number Methods

JavaScript provides a variety of methods that can be used with numbers to perform operations, manipulate data, and format output. Here are some commonly used number methods in JavaScript:

  1. toFixed()

The toFixed() method rounds a number to a specified number of decimal places and returns a string. The argument passed to toFixed() is the number of decimal places to round to:

let num = 3.14159;
console.log(num.toFixed(2)); // Outputs: "3.14"
  1. toPrecision()

The toPrecision() method rounds a number to a specified number of significant digits and returns a string. The argument passed to toPrecision() is the number of significant digits to round to:

let num = 123.456;
console.log(num.toPrecision(4)); // Outputs: "123.5"
  1. toString()

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

let num = 42;
console.log(num.toString()); // Outputs: "42"

The toString() method can also take an optional argument that specifies the base to use when converting the number to a string. The default base is 10, but other bases can be used, such as 2 for binary or 16 for hexadecimal:

let num = 42;
console.log(num.toString(2)); // Outputs: "101010"
console.log(num.toString(16)); // Outputs: "2a"
  1. isNaN()

The isNaN() method determines whether a value is NaN (not a number):

console.log(isNaN("hello")); // Outputs: true
console.log(isNaN(42)); // Outputs: false
  1. isFinite()

The isFinite() method determines whether a value is a finite number:

console.log(isFinite(42)); // Outputs: true
console.log(isFinite(Infinity)); // Outputs: false
  1. parseInt()

The parseInt() method converts a string to an integer. It takes an optional second argument that specifies the base to use when converting the string:

let numStr = "42";
console.log(parseInt(numStr)); // Outputs: 42

let hexStr = "2a";
console.log(parseInt(hexStr, 16)); // Outputs: 42
  1. parseFloat()

The parseFloat() method converts a string to a floating-point number:

let numStr = "3.14";
console.log(parseFloat(numStr)); // Outputs: 3.14

Understanding how to use number methods in JavaScript is essential for performing various operations, manipulating data, and formatting output. By using methods like toFixed(), toPrecision(), toString(), isNaN(), isFinite(), parseInt(), and parseFloat(), you can create more powerful and flexible applications that perform complex calculations and data manipulation.

Javascript

Post navigation

Previous Post: JavaScript BigInt
Next Post: JavaScript Number Properties

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

JavaScript Tutorial

  • JavaScript Tutorial
  • JavaScript Where To
  • JavaScript Output
  • JavaScript Statements
  • JavaScript Syntax
  • JavaScript Comments
  • JavaScript Variables
  • JavaScript Let
  • JavaScript Const
  • JavaScript Operators
  • JavaScript Arithmetic
  • JavaScript Assignment
  • JavaScript Data Types
  • JavaScript Functions
  • JavaScript Objects
  • JavaScript Events
  • JavaScript Strings
  • JavaScript String Methods
  • JavaScript String Search
  • JavaScript Template Literals
  • JavaScript Numbers
  • JavaScript BigInt
  • JavaScript Number Methods
  • JavaScript Number Properties
  • JavaScript Arrays
  • JavaScript Array Methods
  • JavaScript Sorting Arrays
  • JavaScript Array Iteration
  • JavaScript Array Const
  • JavaScript Date Objects
  • JavaScript Date Formats
  • JavaScript Get Date Methods
  • JavaScript Set Date Methods
  • JavaScript Math Object
  • JavaScript Random
  • JavaScript Booleans
  • JavaScript Comparison and Logical Operators
  • JavaScript if, else, and else if
  • JavaScript Switch Statement
  • JavaScript For Loop
  • JavaScript For In
  • JavaScript For Of
  • JavaScript While Loop
  • JavaScript Break and Continue
  • JavaScript Iterables
  • JavaScript Sets
  • JavaScript Maps
  • JavaScript typeof
  • JavaScript Type Conversion
  • JavaScript Bitwise Operations
  • JavaScript Regular Expressions
  • JavaScript Operator Precedence
  • JavaScript Errors
  • JavaScript Scope
  • JavaScript Hoisting: Understanding the Basics
  • JavaScript Use Strict: The Importance of Strict Mode in JavaScript
  • The JavaScript this Keyword
  • JavaScript Arrow Function: A Concise and Comprehensive Guide
  • JavaScript Classes: Understanding the Basics
  • JavaScript Modules
  • JavaScript JSON
  • JavaScript Debugging
  • JavaScript Style Guide
  • JavaScript Best Practices
  • JavaScript Common Mistakes: How to Avoid Them
  • JavaScript Performance: Techniques for Improving Your Code
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions

Copyright © 2023 Buzzingz.

Powered by PressBook WordPress theme