Concise Object Literal Declarations Using Object Property Shorthand in JavaScript.

Concise Object Literal Declarations Using Object Property Shorthand in JavaScript.

With examples.

·

1 min read

Hello everyone, hope you are all doing well. My name is Surya L, and the aim of this blog is to teach you all about Concise Object Literal Declarations Using Object Property Shorthand in JavaScript.

Concise Object Literal Declarations Using Object Property Shorthand

  • ES6 adds some nice support for easily defining object literals.

  • Basically this methods helps us to declare the objects easily by using the short hand property.

Consider the following code:

Example1:

const getMousePosition = (x, y) => ({
  x: x,
  y: y
});
  • A simple function that returns an object with two properties is getMousePosition.
  • ES6 provides syntactic sugar to eliminate the need to write x: x.
  • Simply write x once, and it will be converted to x: x (or something equivalent) under the hood.
  • This is the same function from above, rewritten to use this new syntax:
const getMousePosition = (x, y) => ({ x, y });

Example2:

const createPerson = (name, age, gender) => {
  return {
    name,
    age,
    gender
  };
};

Thanks for reading the blog. Do let me know what you think about it.

You can connect me with Showwcase Twitter Blog GitHub Contact Me

Refernce: I learned this topics in FreeCodeCamp which I explained in minified version