JavaScript - Truncate a string



Truncate a string (first argument) if it is longer than the given maximum string length (second argument). Return the truncated string with a ... ending.


Solution 1 Basic approach with join function


console = { 
    log: print,
    warn: print,
    error: print
};
function truncateString(str, num) {
    var newStr = [];
    for (var i = 0; i < num; i++) {
        if (num >= str.length) {
            return str;
        }
        else { newStr.push(str[i]) }
    }
    newStr = newStr.join("")
    newStr += "..."
    return newStr;
}
console.log(truncateString("A-tisket a-tasket A green and yellow basket", 8));

Output:


A-tisket...

Solution 2 Ternary approach with slice function


console = { 
    log: print,
    warn: print,
    error: print
};
function truncateString(str, num) {
    return str.length > num ? str.slice(0, num) + "..." : str;
}
console.log(truncateString("A-tisket a-tasket A green and yellow basket", 8));

Output:


A-tisket...

download android app

Popular javascript Examples









codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube