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, Functions, etc.
Syntax of Object in JavaScript
// Object literal syntax
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 symbol in ES6+).
- A colon comes after each key, followed by its value.
- They may be of any data type like strings, numbers, booleans, arrays, functions, or other objects.
Top comments (0)