Ruffeng Ruffeng - 4 months ago 10
Javascript Question

Use of hashes to declare a variable in ReactJS/Javascript Es6

I would like to know the difference between:

const data = this.props.content;


And

const {data} = this.props.content;


I saw it before but declaring 3 or 4 variables all together. However I've never seen it with just 1 variable.

What's the difference?

Answer

When writing with just one variable you get the syntactic advantage of using the same name for your variable. I believe the example you listed should be altered slightly which illustrates and hopefully answers your question.

const data = this.props.content; // is exactly the same as
const {content} = this.props;