DEV Community

Jaisurya
Jaisurya

Posted on

JavaScript Objects

In JavaScript, an object is a variable that can hold multiple values. It is a location where a collection of values is stored. Objects are among the most basic data types in JavaScript. Every object contains properties and types, which are a single unit. Objects are not primitive, unlike primitive data types.

For instance, take a football. A football has properties like weight, shape, color, and design. In this case, the football is an object, and its weight and shape are its properties.

Similarly, in JavaScript, objects have properties that define their nature. An object can hold various types, like Strings, Numbers, Booleans, Arrays, Function, etc.

Syntax of Objects in JavaScript

const myObject = {
key1: value1,
key2: value2,
//more key-value pairs
};

Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. Curly braces are used to demarcate the start and termination of an object literal.
  2. Inside these braces, you define key-value pairs separated by commas.
  3. Every key is a string or symbols
  4. A colon comes after each key, followed by its value.
  5. They may be of any data type like stings, numbers, boolean, arrays, functions, or other objects.

Example code:

let person = {
   name:'Jaisurya',
   age: 23,
   city: 'Chennai'
};
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
jonrandy profile image
Jon Randy 🎖️

Every key is a string

This is not actually correct. Keys in an object can be Strings or Symbols 👍️

Collapse
 
jaisurya profile image
Jaisurya

Thanks for the correction