Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this javascript syntax means: const { headers, method, url } = request; [duplicate]

It's not clear what this syntax means

const { headers, method, url } = request;

found in this tut https://nodejs.org/en/docs/guides/anatomy-of-an-http-transaction/

like image 333
user310291 Avatar asked Sep 11 '25 00:09

user310291


1 Answers

It's called destructuring operator.

The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.

Simple example:

var obj={
   "a":2,
   "b":3
}
let {a,b}=obj;
console.log(a,b);

From your example, I saw that request is an object and the statement is translated to

headers = request.headers

But you can also apply destructuring operator for arrays.

var foo = [1, 2, 3];
var [one, two, three] = foo;
console.log(one);
console.log(two);
console.log(three);
like image 138
Mihai Alexandru-Ionut Avatar answered Sep 13 '25 15:09

Mihai Alexandru-Ionut