How to use ES6 arrow function In Javascript
ES6 Arrow functions enable us to write functions with simpler and shorter syntax. We can utilize this function that will return the Volume of a Cube. First we will define a function and pass a (edge) as an argument. we will write the mathematical expression that will return the volume of a cube. Finally, we will call the function.
Syntax:
volCube = (a) => {
return(a * a * a)
}
Example: Below is the function to find Volume of Cube using the arrow function.
volCube = (a) => {
return(a * a * a)
}
console.log('Volume = '+volCube(5));
Output:
Volume = 125
Time complexity: O(1), as the execution time is constant and does not grow with the size of the input.
Auxiliary Space complexity: O(1) as it uses a constant amount of memory regardless of the input size.
JavaScript Program to Find Volume of a Cube
A Cube is a 3-dimensional box-like figure represented in the 3-dimensional plane that has six-shaped faces. It has length, width and height of the same dimension.
The Volume (V) of a Cube can be calculated using the given formula: Volume(V) = a * a * a
Where, a is the Edge of the Cube.
Example:
Input : 10
Output : Volume = 1000
Input : 5
Output : Volume = 125
Table of Content
- Using direct formulae
- Using Math.pow() function
- Using Exponentiation Operator
- Using ES6 arrow function