function truncate(input) {
if (input.length > 5) {
return input.substring(0, 5) + '...';
}
return input;
};
or in ES6
const truncate = (input) => input.length > 5 ? `${input.substring(0, 5)}...` : input;
More Related Contents:
- Count the recurrence of a word
- Repeat a string in JavaScript a number of times
- Check if a JavaScript string is a URL
- How to check if a string contains text from an array of substrings in JavaScript?
- How to remove text from a string?
- How to replace all dots in a string using JavaScript
- How to convert a string to an integer in JavaScript?
- How can I convert a comma-separated string to an array?
- How to set object property (of object property of..) given its string name in JavaScript?
- Shorten string without cutting words in JavaScript
- How to find indices of all occurrences of one string in another in JavaScript?
- How can I insert a character after every n characters in javascript?
- How can I remove a character from a string using JavaScript?
- Javascript – return string between square brackets
- Case insensitive string replacement in JavaScript?
- JavaScript string with new line – but not using \n
- Remove characters from a string [duplicate]
- is the + operator less performant than StringBuffer.append()
- How to extract base URL from a string in JavaScript?
- Get the string representation of a DOM node
- How to convert text to binary code in JavaScript?
- How do I escape a string inside JavaScript code inside an onClick handler?
- Need a basename function in Javascript
- Split a string based on multiple delimiters
- How to trim() white spaces from a string?
- Find difference between two strings in JavaScript
- Get object property name as a string
- Compare Strings Javascript Return %of Likely
- How to add spaces between every character in a string?
- javascript – Convert array to string while preserving brackets