Objects in JavaScript

Full Stack Web Development Courses with Real-time projects Start Now!!

Program 1

<html>
    <head>
        <title>Java Script</title>
        <script type="text/javascript">
            //    // Literals
            // document.writeln("<center>")   
            // var emp={empid:101,empname:"Rajesh Sharma",empsalary:70000,empdept:"CSE"}  
            // document.writeln("<br>"+emp.empid + " " +emp.empname + " "+emp.empsalary + " "+ emp.empdept) 
            // document.writeln("</center>")               
            document.writeln("<center>")   
                // Using new keyword
                // var emp=new Object()
                // emp.empid=101
                // emp.empname='Vishal Verma'
                // emp.empsalary=60000
                // emp.empdept='IT'
                // document.writeln("<br>"+emp.empid + " " +emp.empname + " "+emp.empsalary + " "+ emp.empdept) 

                // Using function constructor

                function employee(empid,empname,empsalary,empdept)
                {
                     this.empid=empid
                     this.empname=empname
                     this.empsalary=empsalary
                     this.empdept=empdept
                    document.writeln("<br>"+this.empid + " " +this.empname + " "+this.empsalary + " "+ this.empdept) 
                }
            document.writeln("</center>")
            
            employee(101,'Amit Verma',60000,'CSE')
         </script>
    </head>
    <body>
        <center>
            
         </center>   
    
    </body>
</html>

Program 2

<html>
    <head>
        <title>Java Script</title>
        <script type="text/javascript">
            document.writeln("<center>")   
                // Array of Object
            let emp=[
                {empid:101,empname:'Vishal Sharma',empdept:'CSE',empqf:['10th','12th','BCA']},  //0
                {empid:102,empname:'Harish Verma',empdept:'IT',empqf:['10th','12th','BE']},  //1
                {empid:103,empname:'Rakesh Gupta',empdept:'CSE',empqf:['10th','12th','MTech']},  //2
                {empid:104,empname:'Sheetal',empdept:'HR',empqf:['10th','12th','MBA']}, //3
                {empid:105,empname:'Sahil',empdept:'IT'}  //4
            ]
            document.writeln("<table border=1>")
            document.writeln("<tr><th>Emp Id</th><<th>Emp Name</th><th>Emp Dept</th><th>Qualification</th></tr>")
            for(let i=0;i<emp.length;i++)
            {
                document.writeln("<tr>")
                document.writeln("<td>" +emp[i].empid+"</td>")  
                document.writeln("<td>" +emp[i].empname+"</td>")  
                document.writeln("<td>" +emp[i].empdept+"</td>")  
                document.writeln("<td>" +emp[i].empqf+"</td>")  
                document.writeln("<tr>")
            }
            document.writeln("</table>")
            document.writeln("</center>")
            
           /* 
              let emp={
                  empid:101,
                  empname:'Rahul Sharma',
                  empsalary:50000,
                  empdept:'IT',
                  empqf:['10','12th','BCA','MCA']
            }     
            document.writeln("Id:"+emp.empid)
            document.writeln("<br>Name:"+emp.empname)
            document.writeln("<br>Salary:"+emp.empsalary)
            document.writeln("<br>Department:"+emp.empdept)
            document.writeln("<br>Qulification:"+emp.empqf)

            let emp1={
                  empid:102,
                  empname:'Vikas Gupta',
                  empsalary:70000,
                  empdept:'CSE',
                  empqf:['10','12th','BCA','MCA']
            }     
            document.writeln("Id:"+emp1.empid)
            document.writeln("<br>Name:"+emp1.empname)
            document.writeln("<br>Salary:"+emp1.empsalary)
            document.writeln("<br>Department:"+emp1.empdept)
            document.writeln("<br>Qulification:"+emp1.empqf)

             
           */
         </script>
    </head>
    <body>
        <center>
            
         </center>   
    
    </body>



</html>

 

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

courses
Image

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *