How To Use Object In JavaScript?


Share:
Published By: Admin, Published On: Dec 13, 2021, Category: Javascript


Hello Friends,

Now let's see example of how to use object example. We will check how to use object. This is a short guide on use object in javascript. Here you will learn how to use object. Let's get started with how to use object in javascript.

Here i will give you many example how to use object using javascript.

Example : 1
<html>
    <head>
        <title>How To Use Object In JavaScript? - MyWebtuts.com</title>
    </head>
    <body>
        <h2>How To Use Object In JavaScript? - MyWebtuts.com</h2>

        <script type="text/javascript">

            var stud = new Object();
            stud.fnm = "Abc";
            stud.lnm = "Xyz";
            stud.id = 2;
            stud.mEng = 88;
            stud.mMaths = 94;
            stud.mSci = 92;

            stud.calAvg = function()
            {
                return (stud.mEng + stud.mMaths + stud.mSci)/3;
            };

            stud.disDetail = function()
            {
                document.write("Student Id: " + stud.id + "<br />");
                document.write("Student Name: " + stud.fnm + " " + stud.lnm + "<br />");
                var avg = stud.calAvg();

                document.write("Student Average Marks: " + avg);
            };
            
            stud.disDetail();
        </script>
    </body>
</html>
Output :
Student Id: 2
Student Name: Abc Xyz
Student Average Marks: 91.33333333333333
Example : 2
<html>
    <head>
        <title>How To Use Object In JavaScript? - MyWebtuts.com</title>
    </head>
    <body>
        <h2>How To Use Object In JavaScript? - MyWebtuts.com</h2>

        <script type="text/javascript">

            function Stud(first, last, id, english, maths, science)
            {
                this.fnm = first;
                this.lnm = last;
                this.id = id;
                this.mEng = english;
                this.mMath = maths;
                this.mSci = science;

                this.calAvg = function()
                {
                    return (this.mEng + this.mMath + this.mSci)/3;
                }

                this.disDatails = function()
                {
                    document.write("Student Id: " + this.id + "<br />");
                    document.write("Student Name: " + this.fnm + " " + this.lnm + "<br />");
                    var avg = this.calAvg();
                    document.write("Student Average Marks: " + avg + "<br /><br />");
                }
            }

            var s1 = new Stud("Jay", "Patel", 1, 98, 75, 88);
            var s2 = new Stud("Henny", "Zala", 3, 86, 96, 74);
            var s3 = new Stud("Kevin", "Patel", 4, 77, 68, 80);
            var s4 = new Stud("Roshan", "Rajput", 2, 97, 67, 75);
            
            s1.disDatails();
            s2.disDatails();
            s3.disDatails();
            s4.disDatails();
        </script>
    </body>
</html>
Output :
Student Id: 1
Student Name: Jay Patel
Student Average Marks: 87

Student Id: 3
Student Name: Henny Zala
Student Average Marks: 85.33333333333333

Student Id: 4
Student Name: Kevin Patel
Student Average Marks: 75

Student Id: 2
Student Name: Roshan Rajput
Student Average Marks: 79.66666666666667

It will help you...

Share: