Skip to content

Instantly share code, notes, and snippets.

@adityatejas
Last active June 20, 2016 15:59
Show Gist options
  • Save adityatejas/7857c0866f67783e71a1c9d60d3beed8 to your computer and use it in GitHub Desktop.
Save adityatejas/7857c0866f67783e71a1c9d60d3beed8 to your computer and use it in GitHub Desktop.
Truncate a string
function truncateString(str, num) {
// Clear out that junk in your trunk
var truncatedStr = '';
if (str.length > num) {
truncatedStr = str.slice(0, num-3) + "...";
return truncatedStr;
}
else if (str.length <= 3)
{
truncatedStr = str.slice(0, num) + "...";
return truncatedStr;
}
else return str;
}
truncateString("Adi", 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment