What does this declaration mean in javascript [duplicate] - javascript

This question already has answers here:
Syntax - what does square brackets around a variable declaration mean [duplicate]
(1 answer)
Multiple assignment in JavaScript? What does `[ a, b, c ] = [ 1, 2, 3 ]` mean?
(4 answers)
Javascript. Assign array values to multiple variables? [duplicate]
(2 answers)
Closed 8 months ago.
Consider the declarations of variable "x" and "y"
const x = 1;
const [y] = [1]
What is the meaning of 2nd declaration?
Can someone suggest an article about such declaration type.

This is destructuring assignment.
You are unpacking values from arrays or object properties.
In your above code, a const variable y will be defined with value 1.

Related

Why are vairable declaration different when we assign the value useState("")? [duplicate]

This question already has answers here:
What does this symbol mean in JavaScript?
(1 answer)
Multiple assignment in JavaScript? What does `[ a, b, c ] = [ 1, 2, 3 ]` mean?
(4 answers)
Closed 9 months ago.
This is what confuses me alot:
When we give a variable a for an example a empty string we do it like this:
const empty = ""
But for useState we do it like this:
const [color, setColor] = useState("Black")
But why? I get so confused that the syntax for useState looks like that.
And ofc searching did not help me.

javascript confusing variable syntax [duplicate]

This question already has answers here:
Javascript - Getting and setting properties on primitives implicitly creates object wrappers
(3 answers)
How is almost everything in Javascript an object?
(6 answers)
adding properties to primitive data types other than Array
(2 answers)
Closed 1 year ago.
I am new to JS, and JS syntax is definitely confusing to me as I'm used to other languages instead.
const x = 5;
x.yy = 3;
console.log(x.yy); // undefined
How is x.yy = 3; a valid syntax?
and why is x.yy undefined when x.yy =3; took in place?

JavaScript destructure an object to another object [duplicate]

This question already has answers here:
One-liner to take some properties from object in ES 6
(12 answers)
ES6 - Destructuring assignment - Unpack some properties from existing object to a new object? [duplicate]
(3 answers)
Closed 4 years ago.
let x = {
a: 10,
b: 20,
c: 30
};
Is there any way to make only a,b keys of x as another object?
let y = x; //someway here through Object destructing
console.log(y); //{a:10,b:20}

Syntax - what does square brackets around a variable declaration mean [duplicate]

This question already has answers here:
Multiple assignment in JavaScript? What does `[ a, b, c ] = [ 1, 2, 3 ]` mean?
(4 answers)
Closed 5 years ago.
Take the following line of code
const [component] = router.getMatchedComponents({ ...to })
Could anyone advise what the square brackets around component means here? I have tried to google this but struggling to find an answer
It's called Destructuring assignment, and it's used to unpack the values of an array and assign them to new variables.
So here in your code:
const [component] = router.getMatchedComponents({ ...to })
You are assigning to the component variable the first element held in the array that will be returned by router.getMatchedComponents({...to}), where to is an array-like structure turned into object using the spread operation.

What is this syntax in JavaScript [duplicate]

This question already has answers here:
Multiple assignment in JavaScript? What does `[ a, b, c ] = [ 1, 2, 3 ]` mean?
(4 answers)
Closed 5 years ago.
Take the following line of code
const [component] = router.getMatchedComponents({ ...to })
Could anyone advise what the square brackets around component means here? I have tried to google this but struggling to find an answer
It's called Destructuring assignment, and it's used to unpack the values of an array and assign them to new variables.
So here in your code:
const [component] = router.getMatchedComponents({ ...to })
You are assigning to the component variable the first element held in the array that will be returned by router.getMatchedComponents({...to}), where to is an array-like structure turned into object using the spread operation.

Categories

Resources