javascript use variable name from string code example

Example 1: get variable name javascript

const myFirstName = 'John'
Object.keys({myFirstName})[0]

// returns "myFirstName"

Example 2: how to name a javascript variable

//variables can include any letter, any number, or the underscore
	//NO SPACES!!! USE UNDERSCORES!!!
	//variable names are case sensitive
	//example:
var change_this = 'whatever you want here'
	//change_this is a variable (change it to your variable name)
	//'whatever you want here' is a string (you'll learn about this later)

Example 3: variable name as a string in Javascript function

var a = [1, 3, 4, 3]
var b = [1, 5, 6, 7]
var c = [3, 4, 8, 9]

function arraySum(x, varName) {
    var sum = 0
    for (let i = 0; i < x.length; i++) {
        sum += x[i]
    }
    console.log("sum of " + varName + " is " + sum)
}

arraySum(a, "a");