I'm looking for a simple script which can truncate a string with ellipsis (...)
I want to truncate something like 'this is a very long string'
to 'this is a ve...'
I don't want to use CSS or PHP.
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;