Skip to content

Instantly share code, notes, and snippets.

@jgresalfi
Created September 14, 2016 01:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgresalfi/8c6ca7e7557fabb7ed21121b03071257 to your computer and use it in GitHub Desktop.
Save jgresalfi/8c6ca7e7557fabb7ed21121b03071257 to your computer and use it in GitHub Desktop.
Truncate a strin...
function truncateString(str, num) {
if (num <= 3) {
str = str.slice(0, num) + "...";
} else if (str.length > num) {
str = str.slice(0, (num - 3)) + "...";
}
return str;
}
truncateString("Jason", 1);
/* Truncate a string (first argument) if it is longer than the given maximum string length (second argument). Return the truncated string with a ... ending.
Note that inserting the three dots to the end will add to the string length.
However, if the given maximum string length num is less than or equal to 3, then the addition of the three dots does not add to the string length in determining the truncated string. */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment