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
};
Explanation:
- Curly braces are used to demarcate the start and termination of an object literal.
- Inside these braces, you define key-value pairs separated by commas.
- Every key is a string or symbols
- A colon comes after each key, followed by its value.
- 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'
};
Top comments (2)
This is not actually correct. Keys in an object can be Strings or Symbols 👍️
Thanks for the correction