JS Utils Kit - v0.5.1
    Preparing search index...

    Function truncate

    • Truncates a string to a specified length, appending a suffix if the string is too long.

      Parameters

      • str: string

        The string to truncate

      • length: number

        The maximum length of the resulting string

      • suffix: string = '...'

        The suffix to append if truncation occurs

        "..."
        

      Returns string

      The truncated string with the suffix if the original string exceeds the length, otherwise the original string.

      console.log(truncate("hello world", 8)); // "hello..."
      console.log(truncate("hi", 5)); // "hi"
      console.log(truncate("hello", 5, "...")); // "hello"
      console.log(truncate("", 3)); // ""