truncate text javascript code example
Example 1: How to make string shorter javascript
const string = "Name".slice(0, 250).concat('...');
const string2 = "Name".substring(0, 250).concat('...');
Example 2: javascript cut string if too long
if (string.length > 25) {
string = string.substring(0, 24) + "...";
}
function truncate(str, n){
return (str.length > n) ? str.substr(0, n-1) + '…' : str;
};
p {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
short = long.replace(/(.{7})..+/, "$1…");
Example 3: javascript truncate string
var string = "ABCDEFG";
var truncString = string.substring(0, 3);
console.log(truncString);
Example 4: determine text truncate javascript
function isTruncated(el) {
return el.scrollWidth > el.clientWidth
}