Skip to content

WebDevHubs

  • Home
  • HTML
  • CSS
  • JavaScript
  • Web Technologies
  • Web Templates
  • Toggle search form

JavaScript Math.sqrt() Method

Posted on February 12, 2025February 12, 2025 By Admin No Comments on JavaScript Math.sqrt() Method

The Math.sqrt() method in JavaScript is used to calculate the square root of a non-negative number.

Syntax

Math.sqrt(x);

Parameters

  • x (Required): A number representing the value for which you want to compute the square root. This number must be non-negative because the square root of negative numbers is not a real number (it could be a complex number). If the argument is negative, the result will be NaN (Not-a-Number).

Return Value

  • Positive Number: If x is a non-negative number, Math.sqrt(x) returns the square root of x.
  • NaN: If x is negative, the method returns NaN (Not-a-Number), because square roots of negative numbers are not real numbers in the realm of real-number arithmetic.

Example 1: Basic example to use Math.sqrt() to get the square root of a number.

let result = Math.sqrt(9);
console.log(result); // Output: 3

result = Math.sqrt(0);
console.log(result); // Output: 0

result = Math.sqrt(1);
console.log(result); // Output: 1

Example 2: Square root of Negative Input

let result = Math.sqrt(-4);
console.log(result); // Output: NaN

In this case, the argument is negative, so Math.sqrt() returns NaN because the square root of a negative number is not a real number.

Example 3: Square Root of a Large Number

let result = Math.sqrt(1000000);
console.log(result); // Output: 1000

The square root of 1,000,000 is 1,000.

Example 4: Square Root of Infinity

let result = Math.sqrt(Infinity);
console.log(result); // Output: Infinity

The square root of Infinity is still Infinity.

Example 5: Using Variables with Math.sqrt()

let x = 16;
let result = Math.sqrt(x);
console.log(result); // Output: 4

You can pass variables to Math.sqrt() as well. In this case, the square root of 16 is 4.

Supported Browsers

BrowserVersion Supported
Chrome1.0+
Firefox1.0+
Safari1.0+
Edge12+
Internet Explorer6+
Opera7.0+
JavaScript, Web Technologies Tags:JavaScript-Math-Method, JavaScript-Method

Post navigation

Previous Post: Top 10 CSS Hacks That Every Developer Should Know In 2025
Next Post: JavaScript Math.tan() Method

Leave a Reply Cancel reply

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

Archives

  • June 2025
  • May 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024

Categories

  • CSS
  • HTML
  • JavaScript
  • Lodash
  • PHP
  • Python
  • Web Technologies
  • Web Templates

Recent Posts

  • JavaScript Array isArray() Method
  • JavaScript Array forEach() Method
  • JavaScript Array includes() Method
  • JavaScript Array keys() Method
  • JavaScript Array lastIndexOf() Method

Recent Comments

No comments to show.

Copyright © 2025 WebDevHubs.

Powered by PressBook Green WordPress theme