How to use a Function In Javascript
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
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:
Table of Content
- Using a Function
- Using Class