Truncate a string-Javascript 1

Truncate a string-Javascript

function truncateString(str, num) {
  return str.length > num ? str.slice(0, num) + "..." : str;
}

Here is what the above code is Doing:
1. If the length of the string is greater than the num, then we slice the string from the beginning to the num and add “…” to the end of the string and return it.
2. If the length of the string is less than or equal to the num, then we just return the string.

Similar Posts