How To Calculate Reading Time in Javascript/Typescript
When you want to calculate how much time needed to read an article
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