var obj = { 0: 'a', 1: 'b', 2: 'c' }; // Creating an object with numerical properties.
console.log(Object.getOwnPropertyNames(obj).sort()); // Logs the property names of obj sorted in ascending order.
// Logging property names and values using Array.forEach
Object.getOwnPropertyNames(obj).forEach(function(val, idx, array) {
console.log(val + '->' + obj[val]); // Logs each property name and its corresponding value.
});
// let's break down the `forEach` method and its parameters:
// 1. **forEach**:
// - `forEach` is a method available on arrays in JavaScript.
// - It iterates over each element of the array and executes a provided function once for each element.
// 2. **function(val, idx, array)**:
// - Here, `function(val, idx, array)` is a callback function provided to `forEach`.
// - This callback function is executed for each element in the array.
// - It takes three parameters:
// - `val`: The current value of the element being processed.
// - `idx`: The index of the current element being processed.
// - `array`: The array on which `forEach` was called (in this case, the array of property names obtained from the object).
// So, when `Object.getOwnPropertyNames(obj).forEach(function(val, idx, array) {...})` is executed:
// - `val` represents each property name of the object `obj` during iteration.
// - `idx` represents the index of the current property name being iterated.
// - `array` represents the array of property names obtained from `obj`. However, in this particular usage, it's not used because `array` represents the array on which `forEach` was called, and we are directly iterating over the property names of the object.