<!-- !
Object -->
Object in JavaScript
Definition:
An object in JavaScript is a collection of key-value pairs.
Each key is a property name (or method name), and the co rresponding value can
be any data type, including other objects or functions.
Syntax:
let objectName = {
key1: value1,
key2: value2,
method1: function() {
// Method code
}
};
<!-- ! methods -->
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:
The `Object.values()` method returns an array of a given object's property
values.
Syntax:
Object.values(object);
3. `Object.entries()`
Definition:
The `Object.entries()` method returns an array of key-value pairs (as arrays)
from an object.
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:
The `Object.fromEntries()` method is opposite of `Object.entries()`, it
converts the array to object.
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:
The `hasOwnProperty()` method returns a boolean indicating whether the object
has the specified property as its own (not inherited) property.
Syntax:
object_name.hasOwnProperty(property);
// ! how to declare object
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"])
// ! how to add element in object
student.phno = 9898777777
console.log(student)
// ! how to modify any key value
student.age = 15
console.log(student)
// ! how to delete element from object
delete student.phno
console.log(student)
// ! how to call any function that is present inside object
student.work()
// ! Methods Of Object
let obj = {
name:"abc",
age:10,
marks:[45,67,89],
isActor:false
}
// ! 1. Object.keys(object_name)
let keys = Object.keys(obj)
console.log(keys)
// ! 2. Object.values(object_name)
let values = Object.values(obj)
console.log(values)
// ! 3 . Object.entries(object_name)
let entries = Object.entries(obj);
console.log(entries)
// ! 4. Object.fromEntries()
let obj2 = Object.fromEntries(entries)
console.log(obj2)
// ! 5. Object.freeze(object_name)
// console.log("before freeze")
// console.log(obj)
// Object.freeze(obj)
// obj.phno = 23455677 // we can't add
// obj.age = 20 // we can't modify
// delete obj.age // we can't delete
// 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')
// obj.phno = 9812345678 // we can't add
// delete obj.age // we can't delete
// obj.age= 20 // yes, we can modify
// 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
let a = 'no 1 in tamil nadu'
'no 2 in tamil nadu'
console.log('5' - 5) //0
if(true)
{
console.log('hhhhhh')
}
console.log(a)
// ! 10. Object.assign()
let obj10 ={
name:"sachin"
}
let obj20 ={
phno:2341234567
}
let combinedObj = Object.assign({},obj10, obj20)
console.log(combinedObj)