JavaScript: Retrieve all the names of object's own and inherited properties
JavaScript Object: Exercise-13 with Solution
Write a JavaScript function to retrieve all the names of object's own and inherited properties.
Sample Solution: -
HTML Code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript function to retrieve all the names of object's own and inherited properties.</title>
</head>
<body>
</body>
</html>
JavaScript Code:
function allKeys(obj) {
if (!isObject(obj)) return [];
var keys = [];
for (var key in obj) keys.push(key);
return keys;
}
function isObject(obj)
{
var type = typeof obj;
return type === 'function' || type === 'object' && !!obj;
}
function Student(name) {
this.name = name;
}
Student.prototype.rollno = true;
console.log(allKeys(new Student("Sara")));
Sample Output:
["name","rollno"]
Flowchart:

Live Demo:
See the Pen javascript-object-exercise-13 by w3resource (@w3resource) on CodePen.
Improve this sample solution and post your code through Disqus
Previous: Write a JavaScript function to parse an URL.
Next: Write a JavaScript function to retrieve all the values of an object's properties.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
JavaScript: Tips of the Day
Creates an object composed of the properties the given function returns truthy for. The function is invoked with two arguments: (value, key)
Example:
const tips_composed = (obj, fn) => Object.keys(obj) .filter(k => fn(obj[k], k)) .reduce((acc, key) => ((acc[key] = obj[key]), acc), {}); console.log(tips_composed({ p: 2, q: '4', r: 6 }, x => typeof x === 'number'));
Output:
[object Object] { p: 2, r: 6 }
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises