Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Access Object Property Dynamically Using Variable in JavaScript</title> </head> <body> <script> // Sample JS object var obj = { name: "Peter Parker", age: 16, country: "United States" } // Property name stored in JS variable var prop = 'name'; // Accessing property value document.write(obj[prop]); // Outputs: Peter Parker </script> </body> </html>