Skip to content

Latest commit

 

History

History
17 lines (15 loc) · 513 Bytes

truncate-text.md

File metadata and controls

17 lines (15 loc) · 513 Bytes
title description author tags
Truncate Text
Truncates the text to a maximum length and appends '...' if the text exceeds the maximum length.
realvishalrana
string,truncate,text
const truncateText = (text = '', maxLength = 50) => {
  return `${text.slice(0, maxLength)}${text.length >= maxLength ? '...' : ''}`;
};

// Usage:
const title = "Hello, World! This is a Test.";
truncateText(title); // Returns: 'Hello, World! This is a Test.'
truncateText(title, 10); // Returns: 'Hello, Wor...'