Objects in JavaScript (lt.35)

Objects in JavaScript (lt.35)

·

2 min read

Introduction:

In JavaScript, an object is a collection of key-value pairs (key and value separated by a colon) where each key is a unique identifier and each value can be of any data type, including other objects, functions, arrays, strings and numbers.

Object has its own attribute and behavior.

Variable v/s object:

Variables

Objects

A variable is a named stored location which holds a value

Object is just a name given to user-defined variables

A variable can be initialized in a usual manner.

Objects are declared and initialized as per the developer’s wish

A variable can contain only one value at a time

An object contains multiple values

Ex. let name, var age.

Ex. let object_name = {Name:"hii", age:21}

Need Objects in JavaScript?

  1. Organizing Data:

  2. Modeling Real-World Entities.

  3. Passing Complex Data.

Creating an object:

//             // this is  the simplest way to create an object
let obj ={
    name : "anonymous",
    id : 1,
    salary:100000
}
console.log(obj)

//                 //
let emp = new Object();    // crearting a new object
console.log(emp)
                // // initialising values to the newly created object
emp.id=10                    
emp.name="new name"
console.log(emp)


//                     //using constructor function

function hi(i , n ,s)
{
this.id=i;          // this is used to access keys insde a object
this.name=n;
this.salary=s;

}
const b = new hi(103,"gopal") 
// new keyword is used to assign values to object created
console.log(b)
console.log(b.name)                 //ways to access values inside the object
console.log(b['name'])
console.log(b['id'])


//                   //  using different object methods

let emp = new Object();    // crearting a new object
console.log(emp)
                           // initialising values to the newly created object
emp.id=10                    
emp.name="new name"
console.log(emp)

console.log(emp.id)       // to access
console.log(emp['id'])

// // // assiging new key vale pair
emp.salary=13000
console.log(emp)
emp['salary']=2000;     // to upadte any value
console.log(emp)

emp.name="hunter"  // method 2 , to  update any value
console.log(emp)

console.log("break")

delete emp.id      // to delete any  key value pair in object
console.log(emp)

Some important object methods

let obje ={
    id:10,
    name:"jack sparrow",
    salary:100000
}
//  //ACCESSING KEY ONLY
let key = Object.keys(obje)
console.log(key)


//  //ACCESING VALUES ONLY
let val  = Object.values(obje)
console.log(val)

//  //ACCESING KEYS AND VALUES TOGETHER
let a = Object.entries(obje)
console.log(a)

console.log("using assign operator")
//   //ASSIGING OLD OBJE VALUES TO A NEW OBJECT
let newOB = Object.assign({},obje)
console.log (newOB)
let newOB1 = Object.assign(obje,{age:21})

console.log (newOB1)

//  // UPDATING VALUES OF OBJECT
console.log("after updation")
obje.id=34
obje['name'] = "captain"
console.log(obje)

// // ADDITION DELETIONA ADN UPDATION IS NOT ALLOWED
Object.freeze(obje)

console.log("after freezing")
obje.id=77       // now it is freezed hence there will be no change
console.log(obje)


//        // SEAL ALLOWS ONLY UPDATION
Object.seal(obje)

lt.34link: https://hashnode.com/post/cltlyfm7q00000al21q4y4qja

Did you find this article valuable?

Support himanshu by becoming a sponsor. Any amount is appreciated!