Skip to content

isEmpty — GTM Variable Template for Condition

VARIABLES › CONDITION
isEmpty EXTENDED Condition

Checks if a value is "empty". Returns true for: empty objects, empty arrays, empty strings, null, undefined, numbers, and booleans.



Examples

Empty object is empty
INPUT
Value To Check: {}
OUTPUT
true
Non-empty string is not empty
INPUT
Value To Check: hello
OUTPUT
false
Null is empty
INPUT
Value To Check: null
OUTPUT
true

GTM Configuration

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

isEmpty
Value To Check
💾 The value to check for emptiness.

Supported formats:
  ✓ Any
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.
Value To Check any
💡 Type any text to see the result update live
🎯 Using special value — click input to type instead
Test with:
Falsy
Truthy
isEmpty()


Under the Hood

📜 View Implementation Code
/**
 * Checks if a value is empty (object, array, string, etc.).
 * 
 * @param {*} data.src - The value to check for emptiness.
 * @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} True if the value is empty, false otherwise.
 *
 * @framework ggLowCodeGTMKit
 */
const getType = require('getType');
const Object = require('Object');

const isEmpty = function(value) {
    if (value == null) {
        return true;
    }
    
    const type = getType(value);
    
    if (type === 'string' || type === 'array') {
        return value.length === 0;
    }
    
    if (type === 'object') {
        return Object.keys(value).length === 0;
    }
    
    if (type === 'number' || type === 'boolean') {
        return true;
    }
    
    return false;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// isEmpty - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const processedValue = applyCast(data.pre, data.src);
return out(isEmpty(processedValue));
// ===============================================================================
// isEmpty() – Apply Mode
// ===============================================================================
/*
return function(value) {
   return out(isEmpty(value));
};
*/
🧪 View Test Scenarios (7 tests)
✅ Test empty string returns true
✅ Test empty array returns true
✅ '[example] Empty object is empty'
✅ '[example] Non-empty string is not empty'
✅ '[example] Null is empty'
✅ Test number returns true (numbers are considered empty)
✅ Test boolean returns true (booleans are considered empty)