Documentation v4.0.0

Overview

Get the type name of a value. Returns a human-readable type string such as "Number", "String", "Array", "Object", "Function", "AsyncFunction", "Symbol", "Null", "Undefined", etc.

Import

import {utils} from 'metronic-extension';
const {typeOf} = utils;

Signature

typeOf(payload: any): string
Parameter Type Description
payload any The value to test.
Returns: string — Type name.

Example

Result: Number
<div class="mb-3">
  <label class="form-label">Select a value</label>
  <select id="typeOfSelect" class="form-select form-select-solid">
    <option value="number">42 (Number)</option>
    <option value="string">'hello' (String)</option>
    <option value="boolean">true (Boolean)</option>
    <option value="object">{} (Object)</option>
    <option value="array">[] (Array)</option>
    <option value="null">null (Null)</option>
    <option value="undefined">undefined (Undefined)</option>
  </select>
</div>
<div>
  <span class="fw-bold">Result: </span>
  <code id="typeOfResult">Number</code>
</div>
var typeOf = metronicExtension.utils.typeOf;
var select = document.getElementById('typeOfSelect');
var result = document.getElementById('typeOfResult');
var testValues = {
  'number': 42,
  'string': 'hello',
  'boolean': true,
  'object': {},
  'array': [],
  'null': null,
  'undefined': undefined,
};

select.addEventListener('change', function() {
  var val = testValues[select.value];
  result.textContent = typeOf(val);
});
// trigger initial
result.textContent = typeOf(42);