Object
Object
Object -->
Object in JavaScript
Definition:
Syntax:
let objectName = {
key1: value1,
key2: value2,
method1: function() {
// Method code
}
};
1. `Object.keys()`
Definition:
The `Object.keys()` method returns an array of a given object's property names
(keys).
Syntax:
Object.keys(object);
2. `Object.values()`
Definition:
Syntax:
Object.values(object);
3. `Object.entries()`
Definition:
Syntax:
Object.entries(object);
4. `Object.freeze()`
Definition:
The `Object.freeze()` method freezes an object, meaning its properties cannot
be added, removed, or modified.
Syntax:
Object.freeze(object);
5. `Object.seal()`
Definition:
The `Object.seal()` similar to `Object.freeze()` but here we can modify the
object.
Syntax:
Object.seal(object);
6. `Object.fromEntries()`
Definition:
Syntax:
Object.entries(object);
7. `Object.assign()`
Definition:
The `Object.assign()` method copies all properties from one or more source
objects to a target object. It returns the modified target object.
Syntax:
Object.assign(target, ...sources);
8. `Object.hasOwnProperty()`
Definition:
Syntax:
object_name.hasOwnProperty(property);
let student = {
name:"bheem",
age:10,
isStudent:true,
subjects:['java','js','html','css'],
work: ()=>{ console.log('hello i am function inside student object')},
address : {
city:"chennai",
pin:785645
}
}
console.log(student)
// ! how to access any element from the object
// by using dot(.)
console.log(student.name)
console.log(student.age)
// by using ["key_name"]
console.log(student["age"])
student.phno = 9898777777
console.log(student)
student.age = 15
console.log(student)
delete student.phno
console.log(student)
student.work()
// ! Methods Of Object
let obj = {
name:"abc",
age:10,
marks:[45,67,89],
isActor:false
}
// ! 1. Object.keys(object_name)
// ! 2. Object.values(object_name)
// ! 3 . Object.entries(object_name)
console.log(entries)
// ! 4. Object.fromEntries()
// ! 5. Object.freeze(object_name)
// console.log("before freeze")
// console.log(obj)
// Object.freeze(obj)
// console.log('after freeze')
// console.log(obj)
// ! 6. Object.isFrozen()
// console.log(Object.isFrozen(obj))
// ! 7 . Object.seal(object_name)
// Object.seal(obj)
// console.log('before seal')
// console.log(obj)
// console.log('after seal')
// console.log(obj)
// ! 8. Object.isSealed(object_name)
// console.log(Object.isSealed(obj))
// ! 9. object_name.hasOwnProperty('key_name')
console.log(obj.hasOwnProperty("subject")) //false
console.log(obj.hasOwnProperty("age")) // true
console.log('5' - 5) //0
if(true)
{
console.log('hhhhhh')
}
console.log(a)
// ! 10. Object.assign()
let obj10 ={
name:"sachin"
}
let obj20 ={
phno:2341234567
}
console.log(combinedObj)