Skip to content

squareRoot — GTM Variable Template for Number

VARIABLES › NUMBER
squareRoot EXTENDED Number

Returns the square root of the provided number. Returns NaN if negative.



Examples

Perfect square root
INPUT
Number: 16
OUTPUT
4
Negative returns NaN
INPUT
Number: -4
OUTPUT
true

GTM Configuration

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

squareRoot
Number
💾 The number to calculate the square root for.

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


Under the Hood

📜 View Implementation Code
/**
* Returns the square root of the provided input.
* 
* @param {number|string} data.src - The number to calculate the square root for.
* @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 calculation.
* 
* @returns {number} The square root of the input number, or NaN if the input is negative or invalid.
*
* @framework ggLowCodeGTMKit
*/
const Math = require('Math');
const makeNumber = require('makeNumber');

const sqrt = function(input) {
   const num = makeNumber(input);
   return Math.sqrt(num);
};

const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);

// ===============================================================================
// squareRoot - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(sqrt(value));
// ===============================================================================
// squareRoot() – Apply Mode: runtime parameter
// ===============================================================================
/*
return function(value) {
  return out(sqrt(value));
};
*/
🧪 View Test Scenarios (5 tests)
✅ '[example] Perfect square root'
✅ Non-perfect square - should return decimal square root
✅ String number - should convert and calculate
✅ Zero - should return zero
✅ '[example] Negative returns NaN'