JavaScript cbrt

The JavaScript cbrt function is one of the Math functions, and it is used to find the cube root of a specified expression or an individual number. The basic syntax of the cbrt Function is:

Math.cbrt(number);

Number: It can be a number or a valid numerical expression.

  • If the number argument is a positive or negative number, the cbrt function will return the cube root of a given value.
  • If the number argument is not a number, it will return NaN.
  • When the number argument is Null, it will return Zero.

JavaScript cbrt Function Example

The cbrt function is used to find the cube root of a given number. In this example, we are going to find the cube root of different data types and display the output

  1. First two statements, we used the JavaScript cbrt Function directly on both positive and negative integers. From the screenshot below, you can observe that it is returning the cube roots.
  2. Next two statements, we used the JavaScript method on both the Positive and negative Decimal values. The cbrt() function is working fine with decimal values as well.
  3. Next statement, we tried the Math method on the String value. As we said before, this will return NaN (Not a Number) as output.
  4. Last, we tried the cbrt() Function on a Null value, and it returned zero as output.
<!DOCTYPE html>
<html>
<head>
    <title> JavaScriptCBRTFunction </title>
</head>
<body>
  <h1> JavaScriptCBRTFunction </h1>
  <p id = "Pos"></p>
  <p id = "Neg"></p>
  <p id = "Dec"></p>
  <p id = "Neg_Dec"></p>
  <p id = "Str"></p>
  <p id = "Null"></p>
  <script>
    document.getElementById("Pos").innerHTML = Math.cbrt(10);
    document.getElementById("Neg").innerHTML = Math.cbrt(-10);
    document.getElementById("Dec").innerHTML = Math.cbrt(14.05);
    document.getElementById("Neg_Dec").innerHTML = Math.cbrt(-6.05);
    document.getElementById("Str").innerHTML = Math.cbrt("JavaScript");
    document.getElementById("Null").innerHTML = Math.cbrt(null);
  </script>
</body>
</html>
JavaScript cbrt Function to find Cube Root

Example 2: In the following example, we use the cbrt() function to find the cube root of 0 and 1.

console.log(Math.cbrt(0));
console.log(Math.cbrt(1));
0
1

Example 3: When we use the cbrt() function on infinity and NaN (not a number) values, it returns the same result.

let a = Infinity;
let b = -Infinity;
let c = NaN;

console.log(Math.cbrt(a));
console.log(Math.cbrt(b));
console.log(Math.cbrt(c));
Infinity 
-Infinity 
NaN

Using the cbrt() function on a numeric string

If the string contains a numeric value, JavaScript automatically converts it to an integer and applies the cbrt() function to it to find the cube root. However, if the given string is non-numeric, it returns NaN.

let a = "125";
let b = "Hi";
console.log(Math.cbrt(a));
console.log(Math.cbrt(b));
5 
NaN

Can Math.cbrt() handle negative numbers?

Yes. The cbrt() function accepts negative values to find the cube root of them and retains the symbol.

console.log(Math.cbrt(-8));
console.log(Math.cbrt(-27));
console.log(Math.cbrt(-64));
-2 
-3 
-4

JavaScript cube root without using Math.cbrt

We can use the Math.pow() function with the second argument as 1/3 to find the cube root of a number. However, it won’t work for negative numbers, so we must use abs() to convert negative numbers to positive.

Next, use the sign() function to present the negative symbol. As the pow() function returns a floating-point number, we must use the round() function to return the nearest integer.

function cubeRoot(n) 
{
return Math.round(Math.sign(n) * Math.pow(Math.abs(n), 1 / 3));
}

console.log(cubeRoot(8));
console.log(cubeRoot(-64));
console.log(cubeRoot(0));
console.log(cubeRoot("Hi"));
2 
-4 
0 
NaN

Related Math Posts

Comments are closed.