function truncateString(str, num) {
return str.length > num ? str.slice(0, num).concat('...') : str;
}
truncateString("A-tisket a-tasket A green and yellow basket", 8);
Can be done with a template literal as well:
return str.length > num ? `${str.slice(0, num)}...` : str;