💾isPrimitive()
check if value is a primitive
JS ⟩ values ⟩ custom functions ⟩ isPrimitive()
replit:isPrimitive()
┌── (primitive ?)
│ typeof type expr value
---------------------------------------------------------------------
✅ object Null null null
---------------------------------------------------------------------
✅ undefined Undefined undefined undefined
---------------------------------------------------------------------
✅ number Number 37 37
✅ number Number 3.14 3.14
✅ number Number Math.LN2 0.6931471805599453
✅ number Number Infinity Infinity ⭐️
✅ number Number NaN NaN ⭐️
✅ number Number Number('1') 1
✅ number Number Number('ab') NaN
---------------------------------------------------------------------
✅ bigint BigInt 42 42n
---------------------------------------------------------------------
✅ string String 'bla' 'bla'
✅ string String `x = ${1+2}` 'x = 3'
✅ string String typeof 1 'number'
✅ string String String({}) '[object Object]'
✅ string String typeof xxx 'undefined'
---------------------------------------------------------------------
✅ boolean Boolean true true
✅ boolean Boolean Boolean(1) true
✅ boolean Boolean !!(1) true
---------------------------------------------------------------------
✅ symbol Symbol Symbol() Symbol()
✅ symbol Symbol Symbol.iterator Symbol(Symbol.iterator)
---------------------------------------------------------------------
❌ object Object {a:1} { a: 1 }
❌ object User user User { name: 'JohnDoe' }
❌ object Array [1, 2] [ 1, 2 ]
❌ object Date new Date() 2022-09-13T01:47:46.344Z
❌ object RegExp /regex/ /regex/
---------------------------------------------------------------------
❌ function Function function(){} [Function (anonymous)]
❌ function Function Math.sin [Function: sin]
❌ function Function () => {} [Function (anonymous)]
❌ function class class {} [class (anonymous)]
❌ function class User [class User]
❌ function GeneratorFunction function*(){} [GeneratorFunction (anonymous)]
---------------------------------------------------------------------
replit:isPrimitive()
// ⭐ check if value is object
function isObject(value) {
return value === Object(value)
}
// ⭐ check if value is primitive
function isPrimitive(value) {
return !isObject(value)
}
// export
module.exports = { isObject, isPrimitive };
isObject() - check if value is an object.
replit:isPrimitive()
require:test cases from typeName()
// ⭐ import (functions for type name)
const { typeName, baseTypeName } = require('./typeNames.js');
const { testCases } = require('./testCases.js');
const { isPrimitive } = require('./isPrimitive.js');
// table settings
const header = ['typeof', 'type', 'expr', 'value'];
const cols = header.length;
const [width, pad, ext] = [12, 1, 18];
const line = '-'.repeat(width*cols + pad*(cols-1) + ext);
// header
console.log(' ', ...header.map(s => s.padEnd(width, ' ')));
console.log(line);
// rows
for (const testCase of testCases) {
// separator
if (testCase === '---') {
console.log(line);
continue;
}
// test cases
const value = testCase[0];
const expr = testCase[1] || String(testCase[0]);
const types = [typeof value, typeName(value)];
const numberOfDifferentTypes = new Set(types.map(s => s.toLowerCase())).size;
console.log(
isPrimitive(value) ? '✅' : '❌',
...[...types, expr].map(s => s.padEnd(width, ' ')),
value,
)
}
console.log(line);
// legend
console.log(`• ⭐️ types not all the same • ❗ base !== type`);
printPrototypeChain() - print the prototype chain of an object.
Last updated