Member-only story

JavaScript Algorithm: Convert Minutes into Seconds

A beginner’s guide to handy time conversions

Max N
2 min readFeb 29, 2024
Photo by Agê Barros on Unsplash

Working with time values in code often requires converting between different units like minutes, seconds, hours, etc. Instead of pulling out your calculator every occasion, let’s write a reusable Javascript function to handle this for us!

In this quick tutorial, we’ll take an integer representing a number of minutes and return it converted to total seconds. Perfect for both practicing simple math logic and handling typical time scenarios in applications.

Defining the Conversion Function

First, we need to define the function signature and minutes parameter:

function convertToSeconds(minutes) {

}

By defining minutes inside those parentheses, we clearly tell the function to accept that value whenever it gets called.

Calculating Total Seconds

Inside the function body, we simply multiply the minute value by 60 — the number of seconds per minute:

function convertToSeconds(minutes) {
var seconds = minutes * 60;
return seconds;
}

Easy as that! We create the variable seconds to store the converted output, multiply the…

--

--

Max N
Max N

Written by Max N

A writer that writes about JavaScript and Python to beginners. If you find my articles helpful, feel free to follow.

No responses yet