JavaScript Object

JavaScript object is a non-primitive data type that allows you to store multiple collection of data. It can store string, number, Boolean, object, array, function as a method,  null, undefined values.


Syntax: 

const obj = {
        name: "Mozahedul",
        age: 23,
        loggedIn: true,
        teacher: ["Abul", "hossain"],
        totalSalary: function(a, b) {return a / b * 100},
        students: {
          name: "Rasel",
          age: 32             
}};


Object Declaration:



Accessing Properties:

With dot notation(.):

With square / bracket notation([ ]):

We *must* use bracket notation when accessing keys that have numbers, spaces, or special characters in them.



Property Assignment:

N.B → if the Property key is string, then we have to change with ([ ]). Otherwise, we have to use (.)


Delete a property:


const obj = {
        name: "Mozahedul",
        age: 23,
        loggedIn: true,
        teacher: ["Abul", "hossain"],          
}};

delete obj.name;


Post a Comment

Previous Post Next Post