How can you get the class name of a JavaScript object?

Both of those are spot on! From my side, after a few years of writing utility libraries, I realized that wrapping the logic into a helper function makes life easier, especially for reusable code where you often need javascript get class name checks.

function getClassName(obj) {
  return obj?.constructor?.name || 'Unknown';
}

const item = new Map();
console.log(getClassName(item)); // "Map"

It’s safe, a bit defensive, and super handy when you’re trying to standardize how you do javascript get class name checks across multiple files or modules. I always tuck this into a utils folder—can’t imagine a project without it now!