How do I use trim() functions in SSJS? Using Data Extensions. Can you please provide some examples?

I have a Data Extension, where "Message" is an Attribute. In this Attribute how do I use the trim() function for removing unwanted spaces?

  • 3
    Do not continue to post hostile and insulting comments against the contributors of SFSE.
    – David Reed
    Nov 19, 2019 at 16:21

3 Answers 3



So I have found out trim() is ECMA 5 and is not available as a native function in SFMC SSJS as that is based on ECMA 3 standards. To be able to utilize, you can use this function to 'backfill' this capability in.

  //Polyfill for trim
  String.prototype.trim = function () {
    return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');

which is utilized like so:

var str = '     TrimMe      ';
var trimmed = str.trim();

Original Answer

Trim is a built in Javascript function. The syntax is slightly different than AMPscript, but still simple.

You just use something like below:

var str = '     TrimMe      ';
var trimmed = str.trim();

Which would output: TrimMe

to remove all spaces in a string, you would need to do the following with a replace:

var str = '  Trim Me  ';
var noSpace = str.replace(/ /g,'');

This would output: TrimMe

The / /g is doing a global replace on all characters. If you do with just ' ' it will only replace the first instance of the space.

  • good, But my idea is- For Example. in My Data extension Having Name = "test ABC Company", value is there. if i am using trim() function. i want to display Name = "testABCCompany". how to do using SSJS .
    – Abdul_SFMC
    Nov 19, 2019 at 14:33
  • 1
    That is not a trim. Trim is only from front and end. I will adjust my answer to provide another solution for that shortly. Nov 19, 2019 at 14:35
  • Thank you So much !!, for giving valuable Suggestion. If any solutions, Please let me know.
    – Abdul_SFMC
    Nov 19, 2019 at 14:38

As far as I know, there is no built in function in SSJS that removes white spaces from a string.

However, you can use AMPScript's function Trim().

In your case, it should be something like:

SET @message = AttributeValue("Message")
SET @trimmedMessage= trim(@message )
  • can you please give some more example...
    – Abdul_SFMC
    Nov 19, 2019 at 13:46
  • 1
    Your question is not clear. You have 3 approches in 3 different answers. If you're not using AMPScript, check out the other answers. Nov 19, 2019 at 15:27

I assume you want to remove the spaces and keep the data in the data extension. When dealing with data extensions, I would suggest you to use SQL instead of SSJS. Extending following code, you will be able to create a Query Activity which reads and updates same data extension, keeping the Message clean from whitespaces, left and right. You need to combine both LTRIM and RTRIM as there is no dedicated TRIM function in the SQL version used in SFMC:

SELECT fieldA, fieldB, RTRIM(LTRIM(Message)) AS Message from DataExtension;

Operations on data within data extensions very rarely require SSJS, and also perform best when used in SQL.

  • i need to use in Cloud Pages using SSJS. not SQL. Thank you for your response.
    – Abdul_SFMC
    Nov 19, 2019 at 14:41
  • @Abdul_SFMC but why not fix the data quality once and for all? So you don't need to trim this string each time it is shown? Nov 19, 2019 at 14:42
  • 2
    That is not trim. That's also not (only) removal of whitespaces. That is also concatenation. Please acquire a better understanding of programming, as SFSE is here to help you where you are stuck, and not provide complete solutions to unclear requests. Nov 19, 2019 at 14:50
  • 4
    One final note, as comments are not for prolonged discussions: 1: You are being unclear in what you want to achieve. 2: Gortonington has already provided an answer to your initial question. 3: Contributors on this forum expect whoever asks questions to do most of the work themselves, and help will be provided when they are stuck. 4: Please acquire basic understanding of SFMC development. Trailhead is a good place to start. 5: Come back to SFSE and ask questions with adequate details and proof of some initial effort from your side, and help WILL be provided. Nov 19, 2019 at 15:03
  • 2
    @Abdul_SFMC - what other steps do you need? That is literally all the steps... I think LukasLunow is 100% right in his final note. This is a place for assistance and community growth, not a free agency to get your work done for you with no effort on your part. Nov 19, 2019 at 15:28

Not the answer you're looking for? Browse other questions tagged .