In JavaScript - ECMAScript 2021, we can get the value of a key in a hash or object using the dot notation or square bracket notation.
Here is an example of getting the value of a key using the dot notation:
const hash = {
name: 'John',
age: 25,
location: 'New York'
};
console.log(hash.name); // Output: John
console.log(hash.age); // Output: 25
console.log(hash.location); // Output: New York
In the above example, we have assigned a hash with three different key-value pairs. We use the dot notation to get the value of the keys.
Here is another example of getting the value of a key using square bracket notation:
const hash = {
name: 'John',
age: 25,
location: 'New York'
};
console.log(hash['name']); // Output: John
console.log(hash['age']); // Output: 25
console.log(hash['location']); // Output: New York
In the above example, we have again assigned a hash with three different key-value pairs. This time, we use the square bracket notation to get the value of the keys.
Both of these methods are equivalent, and we can use either depending on personal preference or situation.