Objects in JS
Object is a non primitive data type to store functions and values. Multiple values as key:value pairs. Functions are stored as key:function() pairs called methods. Object store related information about one thing. Examples: let person = { name : " Priya " , age : 20 , city : " Chennai " }; console . log ( person ); output: {name: "Priya", age: 20, city: "Chennai"} To access object: Dot notation…
An object in JavaScript is a non-primitive data type used to store both functions and values through key-value pairs. Functions within an object take the form of key:function() pairs, known as methods. Objects are designed to encapsulate related information pertaining to a single entity. For instance, let person = { name: 'Priya', age: 20, city: 'Chennai' }; console.log(person); produces the output: {name: 'Priya', age: 20, city: 'Chennai'}.
Accessing an object's properties can be done using either dot notation or bracket notation. For example, console.log(person.name); and console.log(person.age); would output 'Priya' and 20 respectively. Additionally, to access a property stored as a value within an object, such as 'city', one would use console.log(person.city);, resulting in 'Chennai'.
Creating, reading, updating, and deleting (CRUD) operations can be performed on objects. To add a new property, let mobile = { name: 'Vivo', price: 20000 }; mobile.model = 'V80';; console.log(student); would output 'Vivo 20000 V80'. To read a property, console.log(mobile.name); would return 'Vivo', and console.log(mobile['name']); would also return 'Vivo'.
To update a property, mobile.price = 25000; would change the value to 25000, and console.log(mobile.price); would output 25000. Finally, to delete a property, delete mobile.price; would remove the price property, and console.log(mobile); would output 'Vivo V80'.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.