Skip to content

#20 πŸ›‘ JavaScript Objects | JavaScript Object Operations

Adarsh Tripathi edited this page Jul 29, 2021 · 2 revisions

*****************************Tutorial Start πŸ”₯ ********************************

JavaScript Objects, JavaScript Object Operations

What is an object?

javscript is an object based programming language.

  • javaScipt me mostly everthing is an object or act like an object.
  • maximum sab kuch object milega javascript me and kuch cheeze samething like object milegi.
  • agar javascript me work karna hai effeciently too object ki bhut achi samaz honi chahiye, tab hi hum object create kar paayege and maharath haasil kar payege.
  • object is very very imporant topic.

A javScript object is just a collection of key value pairs.

syntax of javascript object--->{key: value}

                              {key:value, key:value}
  • Dono elements ko separate karne ke liye hum , comma use karte hai.
  • Uppar wale object me 2 elements hai: 1. key : value pair(first element), 2. key:value pair(2nd element)

How we create object?

ye object similar like arrays hote hai like,

Arrays me bhi hum collection karte hai, elements ka and object me bhi collection karte hai elements ka, but difference ye hai ki, object me key:value pair ka collection hai and array me simple elements ka collection hai. But array me jo key hoti hai voo index hote hai.

Object ko create karne se pehle ye samazna hoga ki object ke andar jo hum key:value pair likege usko hum {properties} kehte hai.

  • Example: student aik object hai, and student ke andar bhut saari properties hai like, name, phone, address, emailid, college and many more..

and agar iska object hum programming language me create kare too hum isko {} curly braces se create karege.

  <script>
    //student object create
    //these elements are separated by (,) comma and key values are separated by (:)
    //key: value pair object
    let student = {
        Name: "Adarsh",
        phone: "7848264826",
        address: "delhi"
    }
    console.log(student);
</script>

Object can also have function inside!

hum object ke andar, values rakh sakte hai, values rakh sakte hai and hum object ke andar functions ko bhi store kar sakte hai.

  <script>
    //store function in an onject example
    const studentDetails = {
        name: "Akshat",
        phone: 123242524,
        address: "noida",
        //maine display name ka properties liya and usme function defined kar diya
        display: function () {
            console.log(this.name);
            /* (this) yaha pe current object hai and (.) matlab iss object ka jo name hai uski value aajayegi*/
        }

    }
    studentDetails.name;
    studentDetails[name];
//accessing the properties

</script>

object me values ke saath saath hum functions ko bhi store kar sakte hai and hum function ke andar (this) matlab, hum current object use kar sakte hai.

hum object mein pair:values ke saath saath, function bhi rakh sakte hai and uss function ke andar code and logic hi rakh sakte hai.

  • loop through karne ke liye hum use karege for..in loop
  • hum object ki values ko change bhi kar sakte hai baad me

suppose humko kuch properties add karna hai future mai too hum dynamically add kar sakte

suppose humko kuch properties delete karna hai future mai too hum dynamically delete kar sakte

***************************** Tutorial End πŸš€ ********************************

Clone this wiki locally