๐พ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