Member-only story
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…