Get Variable Name as String in JavaScript

In this post, we will see how to get variable name as String in JavaScript.

There are multiple ways to get Get variable name as String in JavaScript. Let’s go through them.

Using Object.keys()

To get variable name as String in JavaScript, use Object.keys() to get the keys array and get the first element.

Output:

Object.keys() returns an array of provided object’s own property names, iterated in same order similar to for loop or while loop.

Using toString() method

To get variable name as String, we can call toString() method and get variable name from it using replace() method to remove unwanted part of the String.

Output:

The replace() method returns a new string with matches of pattern replaced by a replacement string.

It takes 2 parameters.

  1. Regular expression which we want to match in String.
  2. Replacement String – String with which we want to replace matched string.

Using hash table

You can use map in these kind of scenarios where you want to map name to some value.
Here is simple example:

Output:

That’s all about how to get variable name as String in JavaScript.

Was this post helpful?

Related Posts

  • 28 January

    PowerShell – Get Number of Lines in CSV File

    Table of ContentsUsing Get-Content CmdletUsing Import-Csv CmdletUsing Get-ChildItem with Foreach-Object Using Get-Content Cmdlet The Get-Content cmdlet can be used in the following ways to get the number of lines in the specified .csv file: Get-Content cmdlet with .Length property Get-Content cmdlet with .Count property Get-Content cmdlet with .ReadCount property Get-Content cmdlet with Measure-Object cmdlet and […]

  • Read CSV file in PowerShell
    28 January

    Read CSV File in PowerShell

    Table of ContentsUsing the Import-Csv CmdletUsing the ConvertFrom-Csv CmdletUse the ConvertFrom-Csv Cmdlet with the Pipe (|) OperatorUse the ConvertFrom-Csv Cmdlet with the -InputObject Parameter Using the Import-Csv Cmdlet To read a CSV file in PowerShell, use the Import-Csv cmdlet. [crayon-63da590b78640035621454/] [crayon-63da590b78645307946716/] PowerShell provides several cmdlets that allow us to work with CSV files: Import-Csv ConvertFrom-Csv […]

  • Read File into String in PowerShell
    27 January

    Read File into String in PowerShell

    Table of ContentsUsing the Get-Content CmdletUse the Get-Content Cmdlet to Read a Single FileUse the Get-Content Cmdlet with forreach loop to Read Multiple FilesUsing [System.IO.File]::ReadAllText() MethodUse the [System.IO.File]::ReadAllText() Method with foreach loop to Read Multiple FilesUsing the Variable Notation with the Out-String Cmdlet Using the Get-Content Cmdlet We can use the Get-Content cmdlet to read […]

  • Get String between two characters in JavaScript
    27 January

    Get String Between Two Characters in JavaScript

    Table of ContentsUsing substring() MethodUsing slice() MethodUsing split() MethodUsing substr() Method 💡TL;DR Use the substring() method to get String between two characters in JavaScript. [crayon-63da590b78948600751943/] [crayon-63da590b7894b369787325/] Here, we got String between , and ! in above example. Using substring() Method Use the substring() method to extract a substring that is between two specific characters from […]

  • Remove property from Object in PowerShell
    27 January

    Remove Property from Object in PowerShell

    Table of ContentsUsing PSObject.Properties.Remove() MethodUsing -ExcludeProperty Parameter Using PSObject.Properties.Remove() Method To delete a property from an object in PowerShell, Use the New-Object cmdlet to create an object. Use the PSObject.Properties.Remove() method to remove a specific property from an object by providing the property’s name as a string parameter. [crayon-63da590b78c46471261463/] [crayon-63da590b78c49470611948/] In the above code, we […]

  • Create File if not exists PowerShell
    26 January

    Create File If Not Exists in PowerShell

    Table of ContentsUsing the if-else StatementUse the New-Item CmdletUse the Out-File CmdletUsing the try-catch StatementUse the New-Item CmdletUse the Out-File CmdletUsing One-Liners Using the if-else Statement We can use the if-else statement to create a file as follows: Use the New-Item Cmdlet To create a file if it does not exist in PowerShell: Use the […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.