AppDividend
Latest Code Tutorials

# JavaScript Math cbrt() Function Example

JavaScript Math.cbrt() method is used to find the cube root of any number. The Math.cbrt() method can come handy in several programs involving mathematical calculations. The cbrt() is a static method of Math, and it can be used without creating an object.

## JavaScript Math cbrt()

Javascript Math.cbrt() function is an inbuilt function that is used to find the cube root of a number. Parameters: The Math.cbrt() function accepts a single parameter, which is simply a number whose cube root needs to find. It returns a cube root of the given number.

### Syntax

```Math.cbrt(x)
```

### Parameter(s)

The variable x, whose cube root value is to be determined.

### Return Value

The cube root value.

### Polyfill

```Math.cbrt = Math.cbrt || function(x) {
return Math.round(x<0?-Math.pow(-x,1/3):Math.pow(x,1/3));
};
```

See the following figure.

#### Note

1. For a positive finite argument x, cbrt(-x) gives the same value as -cbrt(x).
2. If the argument is negative infinity, then this method returns negative infinity.
3. If the argument is positive infinity, then this method returns positive infinity.
4. If the argument is positive zero or negative zero, then this method returns the same value that is passed.
5. If the passed value is not a valid number, the method returns NaN.
6. If the passed value is null, the method returns 0.

#### Compatibility(Version and above)

2. Firefox v25
3. Edge v12
4. Opera v25
5. Safari v8
6. Android webview
7. Chrome for Android v38
8. Firefox for Android v25
9. Opera for Android
10. Safari on iOS v8
11. Samsung Internet
12. Node.js v0.12

Non-compatible with: Internet Explorer

JavaScript version: ECMAScript 6

## JavaScript cbrt() example

The following code demonstrates the use of a JS cbrt() method.

See the following code.

```// example1.js

var a = 8;
var b = 27;
var c = 64;
var d = -64;

console.log(Math.cbrt(a));
console.log(Math.cbrt(b));
console.log(Math.cbrt(c));
console.log(Math.cbrt(d));```

#### Output

```node example1
2
3
4
-4
```

### Example 2

The following example demonstrates a case where the values other than valid numbers are passed.

```//example2.js

var a = "Hello, world";
var b;

console.log(Math.cbrt(a));
console.log(Math.cbrt(b));
```

#### Output

```node example2
NaN
NaN
```

### Example 3

The js cbrt() method cannot be used with complex arguments as only integer arguments are accepted.

```// example3.js

// Complex values cannot be passed as arguments as follows
// Since only integer arguments are accepted.

console.log(Math.cbrt(2 + i));
```

#### Output

```node example3
ReferenceError: i is not defined```

### Example 4

The following example demonstrates a use of polyfill for the Javascript cbrt() method.

```// example4.js

var a = 8;
var b = 27;
var c = 64;
var d = -64;

function polyfill(x) {
return Math.round(x < 0 ? -Math.pow(-x, 1 / 3) : Math.pow(x, 1 / 3));
}

console.log(Math.cbrt(a));
console.log(Math.cbrt(b));
console.log(Math.cbrt(c));
console.log(Math.cbrt(d));
console.log();

console.log(polyfill(a));
console.log(polyfill(b));
console.log(polyfill(c));
console.log(polyfill(d));```

#### Output

```node example4
2
3
4
-4

2
3
4
-4
```

### Example 5

The following example demonstrates that for positive finite argument x,

The cbrt(-x) gives the same value as -cbrt(x).

See the following code.

```//example5.js

var x = 125;

console.log(Math.cbrt(-x));
console.log(-Math.cbrt(x));
```

#### Output

```node example5
5
5
```

### Example 6

The following example demonstrates that if negative infinity is passed as an argument, then the cbrt() method returns negative infinity.

See the following code.

```// example6.js

var a = Number.NEGATIVE_INFINITY;

console.log(Math.cbrt(a));
```

#### Output

```node example6
Infinity
```

### Example 7

The following example demonstrates that if positive infinity is passed as an argument, then this method returns positive infinity.

```// example7.js

var a = Number.POSITIVE_INFINITY;

console.log(Math.cbrt(a));
```

#### Output

```node example7
Infinity
```

### Example 8

The following example demonstrates that if positive or negative zero is passed as an argument, then this method returns zero with the same sign as the argument.

```// example8.js

var a = 0;
var b = -0;

console.log(Math.cbrt(a));
console.log(Math.cbrt(b));
```

#### Output

```node example8
0
-0
```

### Example 9

The following example demonstrates that if null is passed as an argument, then this method returns 0.

```// example9.js

var a = null;
var b = "";
var c = [];

console.log(Math.cbrt(a));
console.log(Math.cbrt(b));
console.log(Math.cbrt(c));
```

#### Output

```node example9
0
0
0
```