0

In js we can look up the name of a function

var func1 = function() {}
console.log(func1.name) //prints func1

Can i do the same thing for a boolean?

var myMightyBoolean = true
console.log(myMightyBoolean.name) //prints myMightyBoolean, this doesnt work thus the question

Edit: The following allows for it but only under certain conditions, watch comments below or top answer for more

console.log(Object.keys({myMightyBoolean}).pop())

SLuck
  • 521
  • 3
  • 14

1 Answers1

3

No.

The name of a function is a feature of the function.

const foo = function bar () {};
console.log(foo.name);

If you create an anonymous function, then it will get a name from the variable you assign it to at the time of creation.

const foo = function () {};
console.log(foo.name);

But only at the time of creation:

function makeFunction() {
    return function () {};
}

const foo = makeFunction();
console.log(foo.name);

Boolean primitives aren't functions, they don't have names.

Given a set of variables or object properties, you could test each one in turn to find a match for the value of the boolean and then output the name of the variable/property … but that's not the same thing.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335