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...'