
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to access an object value using variable key in JavaScript?
In this article we are going to discuss how to access an object value using variable key in JavaScript.
An object value can be accessed by a Dot Notation and a Bracket Notation. To get the object value through a variable key, the value or expression inside the bracket notation must match with the existing key name, then it returns a value. The bracket notation, unlike the dot notation can be used with variables. If we are using a variable with bracket notation, the variable must reference a string.
Let’s understand this concept better with the help of examples further in this article.
Example 1
The following is an example program that accesses an object value using the Bracket Notation ‘[]’ and Dot Notation ‘.’.
<!DOCTYPE html> <html> <head> <title>To access an object value using variable key</title> </head> <body style="text-align : center"> <h3>Access an object value using variable key.</h3> <p id="key-value"></p> <script> let student = { name : 'Rakesh', roll_no : 48, course : 'Computer Science' }; document.getElementById("key-value").innerHTML = 'The value for student["name"] is : '+student["name"]+'<br/>'+'The value for student.roll_no is : '+student.roll_no+'<br/>'+'The value for student["course"] is : '+student['course']; </script> </body> </html>
On executing the above code, the following output is generated.
Example 2
The below is an example program to access an object value using new variable key name by copying the existing key name to new variable key name.
<!DOCTYPE html> <html> <head> <title>To access an object value using variable key</title> </head> <body style="text-align : center"> <h3>Access an object value using variable key.</h3> <p id="key-value"></p> <script> let student = { name : 'Rakesh', roll_no : 48, course : 'Computer Science' }; let Name = 'name'; let Roll_No = 'roll_no'; let Course = 'course'; document.getElementById("key-value").innerHTML = 'The value for student[Name] is : '+student[Name] +'<br/>'+'The value for student[Roll_No] is : '+student[Roll_No]+'<br/>'+"The value for student[Course] is : "+student[Course]; </script> </body> </html>
On executing the above code, the following output is generated.
Example 3
The below is an example program to access an object value using dot notation. We can write an expression inside the dot notation. The expression inside the dot notation gets computed before the value gets accessed. When the computed expression value matches the original key name, then it returns a value.
<!DOCTYPE html> <html> <head> <title>To access an object value using variable key</title> </head> <body style="text-align : center"> <h3>Access an object value using variable key.</h3> <p id="key-value"></p> <script> let student = { name : 'Rakesh', roll_no : 48 }; let Name = 'na'; let Roll_No = 'roll_'; let Course = 'course'; student[Course] = 'IT'; document.getElementById("key-value").innerHTML = 'The value for student[Name+"me"] is : '+student[Name+'me'] +'<br/>'+'The value for student[Roll_No+"no"] is : '+student[Roll_No+'no']+'<br/>'+"The value for student[Course] is : "+student[Course]; </script> </body> </html>
On executing the above code, the following output is generated.
- Related Articles
- How to access an object having spaces in the object’s key using JavaScript?
- How to access the first value of an object using JavaScript?
- JavaScript: How to Create an Object from Key-Value Pairs
- How to create an object property from a variable value in JavaScript?
- JavaScript - Set object key by variable
- How to access an object through another object in JavaScript?
- How to access cookies using document object in JavaScript?
- How to use a variable for a key in a JavaScript object literal?
- How to get key name when the value contains empty in an object with JavaScript?
- How to access a derived class member variable by an interface object in Java?
- How to sort an object in ascending order by the value of a key in JavaScript?
- How to create an object and access its properties in JavaScript?
- How to access a JavaScript object using its own prototype?
- How to Sort object of objects by its key value JavaScript
- JavaScript: How to remove the key-value pairs corresponding to the given keys from an object?
