Open In App

Surface area of Cube using JavaScript

Last Updated : 14 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Using JavaScript Function

We will create a function that will hold the formula by taking the edge of a cube (a) as a parameter. The function proceeds to calculate the Surface area of the Cube using the formula (6 * a * a). The calculated Surface Area is returned as the result of the function.

Syntax

function surfaceAreaCube(a) {
return 6 * a * a;
}

Example: Below is the function to find the Surface area of the Cube

Javascript
function surfaceAreaCube(a) {
    return (6 * a * a);
}

let a = 10;
console.log('Surface area = ' + surfaceAreaCube(a)); 

Output:

Surface area = 600

Using Arrow function

ES6 Arrow functions enable us to write functions with simpler and shorter syntax. We can utilize this function that will return the Surface area 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 Surface area of a cube. Finally, we will call the function.

Syntax

surfaceAreaCube = (a) => {
return(6 * a * a)
}

Example: Below is the function to find the Surface area of the Cube:

Javascript
surfaceAreaCube = (a) => {
    return (6 * a * a)
}

let a = 10;
console.log('Surface area = ' + surfaceAreaCube(a)); 

Output:

Surface area = 600

Using class

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

Javascript
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.


Similar Reads

JavaScript Program to Find Surface Area of a Cone
Mathematically, the Surface area of the Cone can be calculated using the formula: area = pi * r * s + pi * r^2, Where r is the radius of the circular base, and s is the slant height of the cone. We will calculate the Surface Area of a Cone programmatically using JavaScript. These are the following methods: Table of Content Using Destructuring Assig
2 min read
JavaScript Program to Find Curved Surface Area of a Cone
A cone is a three-dimensional geometric shape. It consists of a base having the shape of a circle and a curved side (the lateral surface) ending up in a tip called the apex or vertex. We will be going to learn how can we calculate it by using JavaScript. Formula used:Curved Surface area of a Cone = πrlExamples: Input: radius = 0.8, slant height = 2
2 min read
JavaScript Program to find Volume & Surface Area of Cuboid
A Cuboid is a 3-dimensional box-like figure represented in the 3-dimensional plane. Cuboid has 6 rectangle-shaped faces that have length, width, and height of different dimensions. We are going to learn how can we calculate the Volume and Surface Area of a Cuboid using JavaScript. Formula used:Surface area of Cuboid = 2 * ((l * h) + (l * b) + (h *
2 min read
JavaScript Program to Calculate the Surface Area of a Triangular Prism
Given base, height, length, side1, side2, and side3, our task is to calculate the surface area of a triangular prism in JavaScript. A triangular prism is a 3D geometric shape with two triangular bases and three rectangular faces. These rectangular faces connect the corresponding sides of the two triangular bases. Example: Input: base = 5; height =
3 min read
CSS Viewer Cube without Three.js using Vanilla JavaScript
Creating a CSS viewer cube without three.js using Vanilla JavaScript offers a lightweight alternative for rendering 3D cubes in web applications. This article explores various approaches to implementing a CSS viewer cube without relying on three.js, providing developers with flexibility and simplicity. These are the following approaches: Table of C
4 min read
Cube Sum of First n Natural Numbers in JavaScript
To find the cube sum, we can iterate over each natural number from 1 to 'n'. For each number, we will cube it (multiply it by itself twice) and add the result to a running total. This means we are going to add up the cubes of each number from 1 to 'n'. There are different methods to cube the sum of the first n natural numbers in JavaScript which ar
3 min read
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 * aWhere, a is the Edge of the Cube.Example: Input : 10Output : Volume = 1000 Input : 5Output : Volum
3 min read
JavaScript Program to Find Cube Root of a Number
The cube root of a number n is a value x such that x3 = n. With JavaScript programming, we can implement an algorithm to find the cube root of a given number using various methods. The goal is to implement a function cube Root(x) that computes the cube root of a given number x using various approaches. Below are the approaches to find the cube root
3 min read
Largest Rectangle Area under Histogram using JavaScript | Without using Stacks
Find the largest rectangular area possible in a given histogram where the largest rectangle can be made of a number of contiguous bars. For simplicity, assume that all bars have the same width and the width is 1 unit. For example, consider the following histogram with 7 bars of heights {6, 2, 5, 4, 5, 1, 6}. The largest possible rectangle possible
2 min read
Glowing Cube loader using HTML & CSS
Glowing cube loader can be created using HTML and CSS. We'll use HTML to create the structure and some CSS properties. The loader is the part of an operating system that is responsible for loading programs and libraries. It is one of the essential stages in the process of starting a program, as it places programs into memory and prepares them for e
3 min read
Article Tags :