1const myFirstName = 'John'
2Object.keys({myFirstName})[0]
3
4// returns "myFirstName"
1//Get variable name as string
2function getVariableName(v) {
3 for (var key in window) {
4 if (window[key] === v)
5 return key;
6 }
7}
8
9//testing
10var someValue = "something not important right now";
11console.log(getVariableName(someValue)); //>> prints "someValue"
12//also works in functions
13function print(a) {
14 console.log(getVariableName(a)); //>> prints "someValue" because of line 16
15 //you'd think it would print "a" but it doesnt
16}
17print(someValue);//>> look at line 14
1var a = [1, 3, 4, 3]
2var b = [1, 5, 6, 7]
3var c = [3, 4, 8, 9]
4
5function arraySum(x, varName) {
6 var sum = 0
7 for (let i = 0; i < x.length; i++) {
8 sum += x[i]
9 }
10 console.log("sum of " + varName + " is " + sum)
11}
12
13arraySum(a, "a");