Open In App

JavaScript Program to Calculate Kinetic Energy if Mass & Velocity are Given

Given a mass M and velocity V of an object, the task is to calculate its Kinetic Energy. Kinetic energy is the energy an object possesses due to its motion. It depends on the object's mass and its velocity.

The formula to calculate kinetic energy is:

Kinetic Energy = 0.5 * M * V2

Below are the approaches for calculating kinetic energy if mass and velocity are given:

Using a Function

In this approach, we define a function calculateKineticEnergy that takes two parameters: mass and velocity. Inside the function, we use the above formula to calculate the kinetic energy and return the result.

Example: The example below shows the demonstration to calculate kinetic energy using Function.

function calculateKineticEnergy(mass, velocity) {
    return 0.5 * mass * Math.pow(velocity, 2);
}

const mass = 5;
const velocity = 10;

const kineticEnergy = calculateKineticEnergy(mass, velocity);
console.log("Kinetic Energy:", kineticEnergy);

Output
Kinetic Energy: 250

Using Class

In this approach we define a class KineticEnergyCalculator with a constructor that takes mass and velocity as parameters. The class has a method calculate that computes the kinetic energy using the formula.

Example: The example below shows the demonstration to calculate kinetic energy using class.

class KineticEnergyCalculator {
    constructor(mass, velocity) {
        this.mass = mass;
        this.velocity = velocity;
    }

    calculate() {
        return 0.5 * this.mass * Math.pow(this.velocity, 2);
    }
}

const mass = 5;
const velocity = 10;

const keCalculator = new KineticEnergyCalculator(mass, velocity);
const kineticEnergy = keCalculator.calculate();
console.log("Kinetic Energy:", kineticEnergy);

Output
Kinetic Energy: 250
Article Tags :