Easy
Working with Object Properties in JavaScript
Description
Working with Object Properties in JavaScript Objects in JavaScript are used to store key-value pairs, where each key (also called a property) maps to a specific value.
In this challenge, you will work with object properties to retrieve and manipulate data.
Your task is to complete the function getPropertyValue, which takes an object and a property name as parameters and returns the value of the specified property.
If the property does not exist in the object, the function should return undefined.
Example Object Structure:
const person = {name: "Alice",age: 25,occupation: "Software Engineer"};
Your Task:
Complete the function getPropertyValue, which takes an object and a property name as parameters and returns the value of the specified property.
Example Usage:
console.log(getPropertyValue({ name: "John", age: 30 }, "name")); // Expected output: "John"console.log(getPropertyValue({ name: "Emma", age: 22 }, "occupation")); // Expected output: undefined
Constraints:
- The first parameter will always be an object.
- The second parameter will always be a string representing the property name.
- If the property does not exist in the object, return undefined.
- You can access object properties using dot notation (obj.property) or bracket notation (obj["property"]).
- Bracket notation is useful when the property name is dynamic or stored in a variable.
- If the property does not exist, return "undefined"
Did you know?
TypeScript is not a language, it is a superset of JavaScript.
Test Cases
Input:
{ "age": 30, "name": "John" }
Expected Output:
"John"