js long string to shorten a code example

Example 1: javascript cut string if too long

if (string.length > 25) {
  string = string.substring(0, 24) + "...";
}

//or 

function truncate(str, n){
  return (str.length > n) ? str.substr(0, n-1) + '…' : str;
};

//or with CSS

p {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

//or with replace

short = long.replace(/(.{7})..+/, "$1…");

Example 2: short string javascript

short = long.replace(/(.{7})..+/, "$1…");