How to use class In Javascript
Classes are similar to functions. Here, a class keyword is used instead of a function keyword. Unlike functions classes in JavaScript are not hoisted. The constructor method in JavaScript is a special method used for initializing objects created with a class. We need to pass the edge (a) as a parameter that will hold the surface area calculated as 6 * a * a. Finally, we will create an object for the class and pass the value (a).
Syntax
class SurfaceArea {
constructor(a) {
this.surfaceArea = 6 * a * a;
}
}
Example: Below is the function to find the Surface area of the Cube
class SurfaceArea {
constructor(a) {
this.surfaceArea = 6 * a * a;
}
}
const cube = new SurfaceArea(10);
console.log('Surface area = ' + cube.surfaceArea);
Output:
Surface area = 600
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.
Surface area of Cube using JavaScript
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. Three sides of a cube meet at the same vertex.
We can find the Surface area of Cube in JavaScript by using different ways including, the JavasScript function, Arrow function, and by using class.
Table of Content
- Using JavaScript Function
- Using Arrow function
- Using class
Contact Us