-2

I came across this code in JS Novice to Ninja.

const form = document.getElementsByTagname('form')[0];

const [input,button] = form.elements;

this create a const called input mapped to the value of the form[0] and another const called button mapped to form[1]

is this:

 const [input,button] = form.elements;

the same as:

const input = form.elements[0];
const button = form.elements[1];

Is that just some shorthand I've never come across? If so can someone tell me what it's called? Or am I misunderstanding what is happening here.

2

1 Answer 1

1

const [input,button] = form.elements;

would actually be the same as

const input = form.elements[0];
const button = form.elements[1];

This is called destructuring and it can also be used for objects.

const {value} = someObject; would be the same as const value = someObject.value;

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.