DEV Community

Cover image for Truncate any string(first argument) at a given num integer (second argument)
Cesare
Cesare

Posted on

1 1

Truncate any string(first argument) at a given num integer (second argument)

The problem:

we have a string (first argument) and we want to truncate it if the length is above the num integer (second argument).

input:

  • truncateString("A-tisket a-tasket A green and yellow basket", 8)

output:

the first 8 elements of the string plus append "..." at the end of the string

  • A-tisket...

Solution 1:

Step 1

under the function declare a conditional if statement:

if (str.length > num)

Step 2

always under the function's curly brackets insert the return statement that stops the function and gives the value of the function:

return str.slice(0,num) + "..."

the above line of code uses the slice() method that takes two integer as argument for the start and stops index positioning. and append the string "..." using the + operator.

The whole function below

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

}
   else {
     return str;
   } 

}

(truncateString("A-tisket a-tasket A green and yellow basket", 8);
Enter fullscreen mode Exit fullscreen mode

Solution2(Using conditional ternary operator):

Basically the same but without the if statement.

With this solution, we reduce of 50% the lines of codes to only one line!

remember ternary operator is one line which must include:

condition? expression if true: expression if false;

The syntax must always include the ? and :

so -> str.length > num ? return str.slice(0,num) + "..." : str;

The code block using ternary operator:

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));
Enter fullscreen mode Exit fullscreen mode

Image of Stellar post

Check out Episode 1: How a Hackathon Project Became a Web3 Startup πŸš€

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

Top comments (0)

Image of PulumiUP 2025

From Cloud to Platforms: What Top Engineers Are Doing Differently

Hear insights from industry leaders about the current state and future of cloud and IaC, platform engineering, and security.

Save Your Spot

πŸ‘‹ Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay