Skip to content

endsWith — GTM Variable Template for String

VARIABLES › STRING
endsWith EXTENDED String

Checks if the given string ends with the specified substring. Returns true if match found, false otherwise.



Examples

String ends with match
INPUT
String To Check: hello world
Search Term: world
OUTPUT
true
No match at end
INPUT
String To Check: hello world
Search Term: hello
OUTPUT
false
Null input returns false
INPUT
String To Check: null
Search Term: test
OUTPUT
false

GTM Configuration

This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.

endsWith
String To Check
Checks if the given string ends with the specified substring. Returns true if match found, false otherwise.

*** String ends with match***
Input: String To Check: hello world
Search Term: world

↪️ Output: true

*** No match at end***
Input: String To Check: hello world
Search Term: hello

↪️ Output: false

*** Null input returns false***
Input: String To Check: null
Search Term: test

↪️ Output: false
Search Term
🔍 The substring to check if it is at the end of the source.

Supported formats:
  ✓ String
Input Setup
Input Function (optional)
⚙️ Optional pre-processing function applied to the input before internal logic (e.g., convert object to string, normalize case). Internal transformations such as case handling will still apply afterward.
Result Handling
Output Function (optional)
⚙️ Optional function to apply to the result before returning it (e.g., str => str + ' €', val => val !== undefined for boolean conversion). Useful for chaining transformations on the output.
String To Check string
💡 Type any text to see the result update live
🎯 Using special value — click input to type instead
Test with:
Falsy
Truthy
Search Term string
endsWith()


Under the Hood

📜 View Implementation Code
/**
* Checks if the given string ends with the specified substring.
* 
* @param {any} data.src - The string or value to check.
* @param {string} data.trm - The substring to check if it is at the end of src.
* @param {Function|string} [data.out] - Optional output handler: function to transform result or string with format.
*
* Direct-mode specific parameters:
* @param {Function} [data.pre] - Optional pre-processor function to transform src before checking.
* 
* @returns {boolean} Returns true if the src ends with the term, otherwise returns false.
*
* @framework ggLowCodeGTMKit
*/
const endsWith = function(searchData, searchTerm) {
   if (searchData == null || typeof searchTerm !== 'string') {
       return false;
   }
   const searchDataString = searchData.toString();
   return searchDataString.substring(searchDataString.length - searchTerm.length) === searchTerm;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// endsWith - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(endsWith(value, data.trm));
// ===============================================================================
// endsWith(...) – Apply Mode
// ===============================================================================
/*
return function(value, searchTerm) {
   searchTerm = data.rp1 ? data.trm : searchTerm;
   return out(endsWith(value, searchTerm));
};
*/
🧪 View Test Scenarios (5 tests)
✅ '[example] String ends with match'
✅ '[example] No match at end'
✅ Empty string with empty term - should return true
✅ Number ending with specified digits - should return true
✅ '[example] Null input returns false'