What is an Object Literals in JavaScript? Usage and Examples

In this article, you’ll learn about object literals in JavaScript, including their definition, usage, and examples.

You’ll understand the distinction between objects and object literals, and grasp the versatility they offer in JavaScript for creating, structuring, and manipulating data.

So what are you waiting for, let’s dive in to enhance your JavaScript skills!

What is an object literal in JavaScript

An object literals is a data type that is utilized for defining objects in JavaScript.

It is a way to create an object in JavaScript using a comma-separated list of key-value pairs enclosed in curly braces.

The keys, also known as properties, can be strings or identifiers, while the values can be any valid expression.

Additionally, object literals are a common way to store data and they can also represent more complicated things like objects, arrays, functions, and regular expressions.

They’re also handy for keeping details about a specific example of something, like its characteristics and what it can do.

Usage and Examples of object literal in JavaScript

For instance, let’s consider the following object literal that contains details about an employee:

let employee = {
    firstName: "Anne",
    lastName: "Smith",
    age: 18,
};

Object literals have the capability to include nested object literals and functions.

For instance, the following object literal holds data about an employee and incorporates a function to display a greeting message:

let employee = {
    firstName: "Anne",
    lastName: "Smith",
    age: 18,
    greet: function() {
        console.log("Hello, my name is " + this.firstName + " " + this.lastName);
    }
};

You can utilize the dot notation to reach its properties and methods, like this:

 this.firstName 

and

this.lastName

By this moment, we are almost done, and if you want to see the output, we are going to use the following:

employee.greet();

And that’s it we’re finally done!

Here’s the complete code:

let employee = {
    firstName: "Anne",
    lastName: "Smith",
    age: 18,
    greet: function() {
        console.log("Hello, my name is " + this.firstName + " " + this.lastName);
    }
};

employee.greet(); // This line calls the greet method and produces the output

In this example, we create an object person with four properties: firstName, lastName, age, and greet.

The first three properties have string and number values, while the last property has a function value.

This function can be called using the dot notation like this:

employees.greet();

Output:

Hello, my name is Anne Smith

Object literals are commonly used in JavaScript for various purposes such as creating objects, passing options to functions, returning multiple values from functions, and defining data structures.

They provide a concise and readable way to create objects with pre-defined properties and methods.

Difference between object and object literal in JavaScript

An object in JavaScript is a group of properties, wherein each property is a key-value pair.

A key can be a string or a symbol, and a value can be any valid JavaScript value.

Objects can be created using the new Object() constructor or the object literal notation {}.

On the other hand, an object literal is a way to create an object using a comma-separated list of key-value pairs enclosed with curly braces {}.

The keys, also known as properties, can be strings or identifiers, while the values can be any valid JavaScript expression.

So, in a simple understanding, an object is a collection of properties, while an object literal is a specific way to create an object using the literal notation {}.

Conclusion

To sum up, object literals are a fundamental way to define and create objects in JavaScript by using key-value pairs enclosed in curly braces {}.

They are versatile and can store various types of data, including nested objects and functions. Object literals are commonly used for creating objects, passing options to functions, and defining data structures in a concise and readable manner.

It’s important to note that while an object is a general collection of properties, an object literal is a specific notation for creating objects in JavaScript.

We are hoping that this article provides you with enough information. That will help you understand the object literals in JavaScript.

If you want to dive into more JavaScript topics, check out the following articles:

Thank you for reading Itsourcecoders 😊.

Frequently Asked Questions

Is JavaScript still worth learning in 2026?
Yes. JavaScript runs on 98% of websites for the front-end, dominates the back-end via Node.js, powers mobile apps through React Native, builds desktop tools through Electron, and is the scripting layer for most AI tooling (LangChain.js, OpenAI SDK, Vercel AI). Whether you target web, mobile, AI, or full-stack capstones, JavaScript is the broadest single language you can learn.
What is the difference between var, let, and const?
var is function-scoped, hoisted to the top of its scope, and can be redeclared, which leads to bugs in modern code. let is block-scoped (only visible inside the nearest {}) and can be reassigned. const is block-scoped and cannot be reassigned, although object contents can still mutate. Default to const for everything, switch to let only when you actually need to reassign, and avoid var in any code written after 2017.
Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds useful features like top-level await, private class fields with the # prefix, and the .at() array method. If you are writing for older browsers (IE11 or older Android WebViews), transpile down with Babel or use a build tool like Vite, esbuild, or webpack.
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard, free, with built-in IntelliSense, debugger, terminal, Git, and a huge extension marketplace (ESLint, Prettier, GitHub Copilot, Tailwind). Install the JavaScript and TypeScript Nightly extension for the latest language features. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, the Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
How do I run JavaScript locally vs in the browser?
In the browser: open DevTools with F12 (or right-click then Inspect), go to the Console tab, type or paste your code, press Enter. For HTML pages, add a script tag pointing to your .js file. Locally with Node.js: download Node from nodejs.org (LTS version), then run node script.js in your terminal from the file folder. Use the same Node setup for backend capstones, API integrations, and scripts that do not need a browser.
What can I build with JavaScript for my BSIT capstone?
Common BSIT capstones in JavaScript: full-stack web apps using React or Vue on the front-end with Node.js and Express on the back-end (MongoDB or MySQL for the database), real-time chat or notification systems using Socket.io, single-page dashboards with Chart.js or D3.js, cross-platform mobile apps with React Native, AI-powered chatbots using OpenAI SDK and LangChain.js, and Chrome extensions for productivity tools. Add Tailwind CSS for the UI and Vercel or Netlify for free deployment.

Caren Bautista


Technical Writer at PIES IT Solution

Responsible for crafting clear, well-structured, and beginner-friendly content across the platform. Handles the writing, proofreading, and editorial review of tutorials, guides, and documentation to ensure every article is accurate, readable, and easy to follow.

Expertise: Technical Writing · Content Creation · Documentation · Editorial Writing · JavaScript · TypeScript · Python · Python Errors · HTTP Errors · MS Excel
 · View all posts by Caren Bautista →

Leave a Comment