DEV Community

Cover image for ECMAScript 6 ⚡ Shorthand Properties & Method Names
Marcos Mendes
Marcos Mendes

Posted on • Edited on

7

ECMAScript 6 ⚡ Shorthand Properties & Method Names

Hey guys, how are you? This is going to be a short post and very straight to the point.

ES6 introduced a new concept/feature called “Property shorthand”, let's see an example of this feature below:

Let's imagine that we need to create an object to store information about a user, such as first name, last name and age:

Previously in ES5 we would do it as follows, we would create the object passing the property and value.

If you noticed, this way there is a lot of repetition, imagine if we needed to create a "Person" object with more information about our user, we would have a lot more repetitions.

So that's when the shorthand property comes into action, the shorthand allows us to do the following: If the property and the variable have the same name, we just have to create the object as follows =>

This way he will understand that first we will pass the property which is "firstName", then he will pass the value, and so on.
That way, we write a lot less code.

Another really cool thing is the way we create functions inside an object using the shorthand, let's create a function to return our user's full name.

Old form:

How to use in ES6:

With the shorthand I no longer need to put the "function" the "getFullName" is enough.

This makes it much easier to read and we write much less.

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

The best way to debug slow web pages cover image

The best way to debug slow web pages

Tools like Page Speed Insights and Google Lighthouse are great for providing advice for front end performance issues. But what these tools can’t do, is evaluate performance across your entire stack of distributed services and applications.

Watch video

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay