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())

5
  • possible duplicate, but didn't find it Commented May 23, 2019 at 9:56
  • The Function object has a name because it is provided by Function.prototype.name Commented May 23, 2019 at 9:59
  • 2
    @MaheerAli Object.keys({myMightyBoolean}).pop() Commented May 23, 2019 at 10:00
  • You can do this using ES6 as per the answer in the question linked above stackoverflow.com/a/39669231/206614 Commented May 23, 2019 at 10:00
  • @JamesCoyle Yes but it doesn't make any sense to me why not just log 'myMightyBoolean' Commented May 23, 2019 at 10:05

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.