
Objects in JS
1) What is an Object? An object stores data in key : value pairs . Syntax let mobile = { brand : " Vivo " , model : " V70 " , price : 25000 }; Here, brand → key "Vivo" → value Accessing Object Values console . log ( mobile . brand ); console . log ( mobile . model ); Output Vivo V70 Real-World Example let student = { name : " Kavitha " , age : 20 , department : " EEE " }; console . log ( student . name ); Output: Kavitha 2) Creating Object Inside an Object This is called a nested object . It means one object is stored inside another object. Example: Mobile Object let mobile = { name : " Vivo " , model : " V70 " , camera : { rear : " 32MP " , front : " 64MP " } }; Here camera itself is another object. Access Nested Object console . log ( mobile . camera . rear ); console . log ( mobile . camera . front ); Output 32 MP 64 MP Real-World Understanding Think like this: A student has an address Address itself has: city state pincode So: let student = { name : " Kavitha " , address : { city :
Continue reading on Dev.to
Opens in a new tab
