w3resource
JavaScript Exercises

JavaScript Object - Exercises, Practice, Solution

JavaScript Object [18 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts.]

1. Write a JavaScript program to list the properties of a JavaScript object. Go to the editor
Sample object:
var student = {
name : "David Rayy",
sclass : "VI",
rollno : 12 };
Sample Output: name,sclass,rollno
Click me to see the solution

2. Write a JavaScript program to delete the rollno property from the following object. Also print the object before or after deleting the property. Go to the editor
Sample object:
var student = {
name : "David Rayy",
sclass : "VI",
rollno : 12 };
Click me to see the solution

3. Write a JavaScript program to get the length of an JavaScript object. Go to the editor
Sample object :
var student = {
name : "David Rayy",
sclass : "VI",
rollno : 12 };
Click me to see the solution

4. Write a JavaScript program to display the reading status (i.e. display book name, author name and reading status) of the following books. Go to the editor

var library = [ 
   {
       author: 'Bill Gates',
       title: 'The Road Ahead',
       readingStatus: true
   },
   {
       author: 'Steve Jobs',
       title: 'Walter Isaacson',
       readingStatus: true
   },
   {
       author: 'Suzanne Collins',
       title:  'Mockingjay: The Final Book of The Hunger Games', 
       readingStatus: false
   }];

Click me to see the solution

5. Write a JavaScript program to get the volume of a Cylinder with four decimal places using object classes. Go to the editor
Volume of a cylinder : V = πr2h
where r is the radius and h is the height of the cylinder.
Click me to see the solution

6. Write a Bubble Sort algorithm in JavaScript. Go to the editor
Note : Bubble sort is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted,
Sample Data: [6,4,0, 3,-2,1]
Expected Output : [-2, 0, 1, 3, 4, 6]
Click me to see the solution

7. Write a JavaScript program which returns a subset of a string. Go to the editor
Sample Data: dog
Expected Output: ["d", "do", "dog", "o", "og", "g"]
Click me to see the solution

8.Write a JavaScript program to create a Clock. Go to the editor
Note: The output will come every second.
Expected Console Output :
"14:37:42"
"14:37:43"
"14:37:44"
"14:37:45"
"14:37:46"
"14:37:47"
Click me to see the solution

9.Write a JavaScript program to calculate the area and perimeter of a circle. Go to the editor
Note : Create two methods to calculate the area and perimeter. The radius of the circle will be supplied by the user.
Click me to see the solution

10.Write a JavaScript program to sort an array of JavaScript objects. Go to the editor
Sample Object :

var library = [ 
   {
       title:  'The Road Ahead',
       author: 'Bill Gates',
       libraryID: 1254
   },
   {
       title: 'Walter Isaacson',
       author: 'Steve Jobs',
       libraryID: 4264
   },
   {
       title: 'Mockingjay: The Final Book of The Hunger Games',
       author: 'Suzanne Collins',
       libraryID: 3245
   }];

Expected Output:

[[object Object] {
  author: "Walter Isaacson",
  libraryID: 4264,
  title: "Steve Jobs"
}, [object Object] {
  author: "Suzanne Collins",
  libraryID: 3245,
  title: "Mockingjay: The Final Book of The Hunger Games"
}, [object Object] {
  author: "The Road Ahead",
  libraryID: 1254,
  title: "Bill Gates"
}]

Click me to see the solution

11.Write a JavaScript function to print all the methods in an JavaScript object. Go to the editor
Test Data :
console.log(all_properties(Array));
["length", "name", "arguments", "caller", "prototype", "isArray", "observe", "unobserve"]
Click me to see the solution

12.Write a JavaScript function to parse an URL. Go to the editor
Click me to see the solution

13.Write a JavaScript function to retrieve all the names of object's own and inherited properties. Go to the editor
Click me to see the solution

14.Write a JavaScript function to retrieve all the values of an object's properties. Go to the editor
Click me to see the solution

15.Write a JavaScript function to convert an object into a list of `[key, value]` pairs. Go to the editor
Click me to see the solution

16.Write a JavaScript function to get a copy of the object where the keys have become the values and the values the keys. Go to the editor
Click me to see the solution

17.Write a JavaScript function to check if an object contains given property. Go to the editor
Click me to see the solution

18. Write a JavaScript function to check whether a given value is a DOM element. Go to the editor
Click me to see the solution

More to Come !

* To run the code mouse over on Result panel and click on 'RERUN' button.*

Live Demo:

See the Pen javascript-common-editor by w3resource (@w3resource) on CodePen.


Many of our valued users post comments along with piece of code. Disqus may, false positively, consider those piece of code as Bad or Starnge syntax and send those comments to spam automatically. Even if that happens, we will make sure those useful comments are taken out of spam and approved. It may take a couple of days for that though, but any useful comment will be brought to public view for sure. We regret if that happend to any user. You may write us directly regarding this to - w3resource[at]yahoo[dot]com.