How To Calculate Reading Time in Javascript/Typescript
When you want to calculate how much time needed to read an article
Written on November 01, 2023 · 1 min read
Function:
const calculateReadingTime = (text: string) => {
// Average human reading speed (WPM)
const wordsPerMinute = 200
let textLength = String(text).split(" ").length
if (textLength > 0) {
return Math.ceil(textLength / wordsPerMinute)
}
return 0
}
Usage example:
const text = "Lorem ipsum dolor sit amet"
const readingTime = calculateReadingTime(text) // 1
This note is written by Diky Hadna — Software Engineer & Digital Nomad Mentor. Read my story and get in touch with me!