Skip to main content

Command Palette

Search for a command to run...

[JavaScript] Object

Updated
[JavaScript] Object

Object

JavaScript's data types can be broadly classified into primitive types and object types.

  • Primitive types represent a single value, and the values of primitive types are immutable or unchangeable.

  • Object types are complex data structures composed of various types of values as a single unit, and the values of object types are mutable or changeable.

JavaScript is an object-based programming language, and almost everything in JavaScript is composed of objects. An object is a collection of zero or more properties where each property is composed of a key and a value.

When a function is declared as a value in the object, it is called a method.

Object Literal

A literal is a notation used to create a value using human-readable characters or agreed-upon symbols.

In JavaScript, an object literal is a notation used to create an object. It can be written with {}.

let objectLiteral = {
    key: 'Value', // Property
    helloWorld: function () { // Method
        return "Hello world";
    }
};

console.log(objectLiteral.helloWorld()); // Hello world