JavaScript Math.cbrt() Method

The Math.cbrt function in JavaScript calculates the cube root of a specified number. In contrast to the Math.sqrt function, which only handles non-negative values, Math.cbrt can also process negative inputs. The cube root of a number is mathematically denoted by Math.cbrt(x) = y, where y raised to the power of three equals x (y³ = x).

Syntax

The syntax for the cbrt method is outlined as follows:

Math.cbrt(x);

Parameter

x: This represents the value for which we seek the cube root.

Return Values:

The value returned is the cube root of a specified number.

Examples of JavaScript Math.cbrt method

In this section, we will explore the cbrt function by examining multiple examples.

Example 1: Printing the cube root of the given number

Let’s examine an illustration that demonstrates how to compute the cube root of a specified number.

Example

Example

console.log(Math.cbrt(27));  

console.log(Math.cbrt(-64));

Output:

Explanation:

In this illustration, we will demonstrate how to compute and display the cube root of a specified number. The Math.cbrt method in JavaScript calculates the cube root of a numerical value. For instance, invoking Math.cbrt(27) yields the cube root of 27, resulting in an output of 3. Similarly, when Math.cbrt(-64) is executed, it computes the cube root of -64, producing an output of -4.

Example 2: Calculating the cube root of a number entered by the user.

We will explore the method to determine the cube root of a number provided by the user.

Example

Example

<!DOCTYPE html>

<html>

<head>

<title>Calculating the cube root of a number</title>

</head>

<body>

<form>  

  Enter a number: <input type="text" id="num">  

  <input type="button"  onclick="display()" value="submit">  

  </form>  

  <p><span id="result"></span></p>  

<script>  

function display()  

{  

  var x=document.getElementById("num").value;  

document.getElementById("result").innerHTML=Math.cbrt(x);  

}  

</script>  

</body>

</html>

Output:

After a value is entered by a user:

Explanation:

In this illustration, we will demonstrate how to compute the cube root of a number provided by the user and present the outcome upon clicking a button. In the HTML code, we have established a text input field that allows the user to input a number. This input field is assigned an identifier, num. Additionally, a button marked Submit has been incorporated into the form. Once the button is clicked, it invokes the display function through the onclick event. The <span> tag, which is identified by the id result, is utilized to showcase the cube root result on the webpage following the completion of the calculation.

Within the script section, we defined a function called display that is triggered when the user clicks on the button. This function retrieves the value inputted by the user in the designated input field by using document.getElementById("num").value, and assigns it to the variable x. Subsequently, it computes the cube root of x by employing Math.cbrt(x) and displays the outcome in the HTML element identified by the id result.

Example 3: Calculating cube roots of positive numbers

Let's examine an illustration of how to compute the cube roots of positive integers.

Example

Example

var result = Math.cbrt(27);

console.log(result);

Output:

Explanation:

In this instance, we will demonstrate how to compute the cube root of a positive integer. We established a variable called result using the var keyword, assigned it the value of Math.cbrt(27), and then displayed the result in the console. Therefore, upon executing the code, it outputs 3, which represents the cube root of 27.

Example 4: Handling negative numbers

Let's see an example to handle a negative number.

Example

Example

var negativeResult = Math.cbrt(-8);

console.log(negativeResult);

Output:

Explanation:

In the preceding illustration, we will explore the method for computing the cube root of a negative value. We created a variable called negativeResult using the var keyword, assigned it the value of Math.cbrt(-8), and then outputted the result to the console. Consequently, upon executing the code, it yields -2, which represents the cube root of -8.

Example 5: Dealing with zero and infinity

Let’s explore a scenario involving the concepts of zero and infinity.

Example

Example

console.log(Math.cbrt(0)); // Outputs 0

console.log(Math.cbrt(Infinity)); // Outputs Infinity

console.log(Math.cbrt(-Infinity)); // Outputs -Infinity

Output:

Output

0

Infinity

-Infinity

Explanation:

In this illustration, we will explore the concepts of Zero and Infinity. The function Math.cbrt(0) yields 0, as the cube root of 0 is indeed 0. Conversely, the function Math.cbrt(Infinity) results in Infinity, since the cube root of infinity is Infinity. Additionally, the function Math.cbrt(-Infinity) produces -Infinity because the cube root of -Infinity is -Infinity.

Example 6: Working with non-numeric inputs

Let’s explore the process of determining the cube root for values that are not strictly numerical in nature.

Example

Example

console.log(Math.cbrt('27')); // Converts string to number and calculates

console.log(Math.cbrt('a')); // NaN, as 'a' cannot be converted to a number

console.log(Math.cbrt(NaN)); // Outputs NaN

Output:

Output

3

NaN

NaN

Explanation:

In this illustration, we are focusing on Non-Numeric Inputs and outputting the results to the console. In the statement console.log(Math.cbrt('27')), the string '27' is transformed into a numeric value, allowing the calculation of the cube root of 27. Conversely, in console.log(Math.cbrt('a')), the character 'a' is a string that cannot be successfully converted into a numeric value, which leads JavaScript to attempt the conversion but ultimately fail, resulting in NaN. Finally, in console.log(Math.cbrt(NaN)), we directly pass NaN to the Math.cbrt function, and any mathematical computation involving NaN yields NaN. Hence, the final output is NaN.

Conclusion

In JavaScript, the Math.cbrt function is a fundamental part of the Math object, designed for determining the cube root of a specified number. This function is capable of processing both positive and negative numbers, and if a string representation of a number is provided, it will convert that string into a numeric value. Should the input be unconvertible or if NaN is supplied, the function will return NaN. This method finds its primary application in both mathematical and scientific computations. It is important to note that the original input remains unchanged; the function merely produces and returns the calculated output. In this article, we will explore this function through various examples, and we have included a section on frequently asked questions to enhance your understanding of the topic.

Frequently Asked Questions (FAQs):

  1. What is Math.cbrt?

In JavaScript, the Math.cbrt function is a static method that calculates the cube root of a specified number. The cube root of a number x can be expressed as a number y, which satisfies the equation y y y = x.

  1. What is the procedure for using Math.cbrt?

The syntax for the Math.cbrt function is Math.cbrt(x), where x represents the value for which you wish to determine the cube root.

  1. What types of values can be supplied to Math.cbrt?

Any type of value may be supplied to the Math.cbrt function, including positive numbers, negative numbers, or zero. Additionally, it can accept Infinity, -Infinity, and null.

  1. What result does Math.cbrt produce for negative numbers?

The cube root of a negative number is provided by the function. For instance, executing Math.cbrt(-27) yields -3.

  1. What will be the resulting output if we supply a non-numeric argument to Math.cbrt?

When a non-numeric argument is supplied to Math.cbrt, the function will yield NaN as the result.

Input Required

This code uses input(). Please provide values below: