Skip to content

JavaScript object literal | Basics

  • by

JavaScript object literal is a comma-separated list of name-value pairs inside of curly braces. Object literals encapsulate data, enclosing it in a tidy package.

The following demonstrates an example object literal:

var myObject = {
    name: 'John',
    age: 20,
    active: false

The object literal patterns are simple and easy to create objects. ES6 makes the object literal more succinct and powerful by extending the syntax in some ways.

JavaScript object literal

Simple example code of an object literal with one property and one function.

<!DOCTYPE html>
    var greeting = {
      fullname: "John",
      greet: (message, name) => {
        console.log(message + " " + name + "!!");



JavaScript object literal

Object property initializer shorthand

function createMachine(name, status) {
    return {
        name: name,
        status: status

Object Literal Syntax

Object literals are defined using the following syntax rules:

  • A colon separates property name from value.
  • A comma separates each name-value pair from the next.
  • A comma after the last name-value pair is optional.

Do comment if you have any doubts or suggestions on this JS object basics tutorial.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *