What is the object type?

Technology CommunityCategory: JavaScriptWhat is the object type?
VietMX Staff asked 3 years ago

The object type refers to a compound value where you can set properties (named locations) that each hold their own values of any type.

 let obj = { 
   a: "hello world", 
   b: 42, 
   c: true   
 }; 
 
 obj.a; // "hello world", accessed with doted notation 
 obj.b; // 42 
 obj.c;  // true 
 obj["a"]; // "hello world", accessed with bracket notation 
 obj["b"]; // 42 
 obj["c"]; // true 
       

Bracket notation is also useful if you want to access a property/key but the name is stored in another variable, such as:

 let obj = { 
   a: "hello world", 
   b: 42  }; 
 let b = "a"; 
 
 obj[b]; // "hello world" 
 obj["b"]; // 42