Skip to content
Advertisements

JavaScript Object: Examples

Example-1: Object usage within a list.

let library = [
             {
                 title: "Harry Potter",
                 author: "J. K. Rowling",
                 status: true
             },
             {
                 title: "the lord of kings",
                 author: "Peter Jackson",
                 status: true
             },
             {
                 title: "5.wave",
                 author: "Rick Yancey",
                 status: false
             }
         ];
         
         for (var i = 0; i < library.length; i++) {
             var book = library[i].title + "author " + library[i].author + ".";
             if (library[i].status) {
                 document.write(book + "  (read)<br>");
             } else {
                 document.write(book + "  (unread)");
             }
         }

Example-2:

<div class="container">
    <input type="text" id="name" placeholder="name"/>
    <input type="text" id="surname" placeholder="surname"/>
    <input type="text" id="age" placeholder="age"/>
    <input type="button" onclick="people();" value="add and show"/>
    <div id="show"></div>
</div>

 <script>
     function people(name, surname, age) {
         var name, surname, age, show;

         name = document.getElementById("name").value;
         surname = document.getElementById("surname").value;
         age = document.getElementById("age").value;
         show = document.getElementById("show");

         this.name = name;
         this.surname = surname;
         this.age = age;

         write = "name    : " + name + "<br>" +
                 "surname : " + surname + "<br>" +
                 "age     : " + age;
         show.innerHTML = write;
     }
 </script> 

Example-3 :

    <div class="container">
        <input type="button" value="person1" onclick="person1();" />
        <input type="button" value="person2" onclick="person2();" />
        <input type="button" value="person3" onclick="person3();" />
    </div>

     <script>
         
         var persons = {
             name: ["pater", "sara", "oliver"],
             age: [32, 27, 24],
             gender: ["male", "female", "male"],
             country: ["germany", "england", "france"]
         }
         function person1() {

             var bio = "name   : " + persons.name[0] + "\n" +
                       "age    : " + persons.age[0] + "\n" +
                       "gender : " + persons.gender[0] + "\n" +
                       "country: " + persons.country[0];
             alert(bio)
         }
         function person2() {

             var bio = "name   : " + persons.name[1] + "\n" +
                       "age    : " + persons.age[1] + "\n" +
                       "gender : " + persons.gender[1] + "\n" +
                       "country: " + persons.country[1];
             alert(bio)
         }
         function person3() {

             var bio = "name   : " + persons.name[2] + "\n" +
                       "age    : " + persons.age[2] + "\n" +
                       "gender : " + persons.gender[2] + "\n" +
                       "country: " + persons.country[2];
             alert(bio)
         }

     </script>   
Advertisements

1 Comment »

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: