Whatever message this page gives is out now! Go check it out!

add

Last update:
May 18, 2026
Adds an element to the set; duplicates are ignored.

Description

Inserts a value into the set. If the value is already present, If the value is already present (according to set equality rules), it is ignored. Sets never contain duplicates.

Returns

Void or the set instance.

Category

ColdFusion Set member methods

Function syntax

set.add(element)

Parameters

ParameterDescription
elementThe value to add.

See also

Example

The following example uses this API in cfscript (syntax: set.add(element)).

<cfscript>
    s = setNew();
    s.add("apple");
    s.add("banana");
    s.add("apple"); // duplicate ignored
    writeOutput(s.size()); // 2
</cfscript>
      

Real-world example

Build a normalized set of valid email recipients from a messy import list.

<cfscript>
    function buildRecipientSet(required array rawAddresses) {
        var recipients = setNew("nocase");
        for (var addr in rawAddresses) {
            var clean = trim(lcase(addr));
            if (isValid("email", clean)) {
                recipients.add(clean);
            }
        }
        return recipients;
    }

    raw = [ "A@EXAMPLE.COM", "a@example.com", "bad@", "b@example.com" ];
    recips = buildRecipientSet(raw);
    writeOutput(recips.size()); // 2 unique
</cfscript>
      

Share this page

Was this page helpful?
We're glad. Tell us how this page helped.
We're sorry. Can you tell us what didn't work for you?
Thank you for your feedback. Your response will help improve this page.

On this page