As an experiment with string literal templates, I was trying to insert the variable’s name into the template, not the value. Is this possible?
My code:
let testArr = [1, 2, 3, 4];
let arrAssess = (x) => {
let a = x.length;
return `$[x] Is an array with $[a] indexes.`;
}
With the code like this, arrAssess(testArr); returns ‘1,2,3,4 is an array with 4 indexes.’
Is there any way to make it return ‘testArr is an array with 4 indexes.’ ?
I do not believe this is possible; variable names are arbitrary as far as the computer is concerned (though they should make sense to human readers). As far as I know, you wanted something like this, you’d have to add some sort of ‘name’ property to an object.
In Python, there is method all objects can implement that provides a way to display the object. That’s the closest thing that comes to mind.