0

Saw this code recently and can't seem to find the answer on google, so here we are.

The actual code was from a webpack config:

const {
  NODE_ENV = 'production',
} = process.env;

Thanks for any help!

Sean
  • 103
  • 6
  • 1
    [Duplicate](https://google.com/search?q=site%3Astackoverflow.com+js+equal+sign+in+curly+brackets) of [Javascript object bracket notation ({ Navigation } =) on left side of assign](/q/26999820/4642212) and [Javascript Object destructuring and default parameters combined](/q/51697735/4642212). See [What does this symbol mean in JavaScript?](/q/9549780/4642212) and the documentation on MDN about [expressions and operators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators) and [statements](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements). – Sebastian Simon Apr 25 '21 at 18:28

3 Answers3

3

Destructuring with a default value.

const { NODE_ENV } = process.env;

// is roughly:

const NODE_ENV = process.env.NODE_ENV

And together

const { NODE_ENV = 'production' } = process.env;

// is roughly (probably presence check is more sophisticated):

const NODE_ENV = process.env.NODE_ENV || 'production' 

More info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

Balázs Édes
  • 12,013
  • 4
  • 45
  • 79
2

This mean you assign default value to variable. look example below.

const { foo = "bar" } = "baz";
// foo -> "bar"


const { foo = "bar" } = { foo: "baz" };
// foo -> "baz"
Ahmed ElMetwally
  • 1,707
  • 2
  • 3
  • 11
1

This syntax assigns NODE_ENV the value process.env.NODE_ENV, or if that is falsy, 'production'.. This is called destructuring and a few examples can be seen below:

Get key from object using default name

const my_object = {message: "hello"};
const {
  message
} = my_object;

Get key from object using custom name

const my_object = {message: "hello"};
const {
  message: my_message
} = my_object;