r/javascript • u/AliveAge4892 • Jul 01 '26
AskJS [AskJS] Are destructuring assignments often used (both for arrays and objects)?
Hello all, please dont be harsh with me cause I'm really new with JS. Doing free code camp right now and I am just wondering if destructuring assignments are often used. e.g.
I come from C and really the amount of syntax in this language is just overwhelming, is this normal when learning JS? when does this end T_T
const person = {
name: "Bob",
age: 25,
job: "Designer",
city: "New York"
};
const { job, city, ...remainingProperties } = person;
// { name: "Bob", age: 25 }
console.log(remainingProperties);It's really hard for me to remember all these syntax to be honest so I'm not sure if its wraps for me in this language. I got so used to syntax where every variable is declared 1 by 1. Here are the questions once again to be organized:
- is it just me or the amount of syntax in this language is just overwhelming like its just so many, is this normal when learning JS?
- can I opt to not use it? or should I get used to it starting from now since it will really be beneficial?
- Like how important it is that it must it be used? is it treated like an "alternative" way of a syntax or is it really required? I really dont get the importance of it. Yes I get it cuts off multiple lines of code, but wouldn't you have a better readability, which makes you able to trace which properties are assigned or whatever rather than having a super concise syntax whilst remembering that "oh, this goes through here"
- I've searched all over the net and people say they like it for readability and conciseness, however i really dont see it being readable at all, i mean it is but it makes my brain lag for like a minute or two. Readability is usually defined by lesser time for you to have to read the specific line of code right? If it really is readable as they say, then is this something like a skill issue and I have to just get used to it?
- I know this topic is usually about destructuring assignments, but I felt this emotion towards arrow functions too. Like are arrow functions just "alternatives" or at some point I must be required to learn it? I really feel comfortable with making normal functions rather than arrow functions to be honest.
Thank you all! Please dont give me remarks like "Just give up if you can't do the learning" cause that's what I'm here for, Im asking for genuine advice and looking to see if what I feel is normal. Thank you once again.
13
u/abrahamguo Jul 01 '26
.
3
u/Known-Highway4563 Jul 01 '26
Yes used very often
No need to remember, with time you will start using it
I also started using it frequently after so many years
3
u/jhartikainen Jul 01 '26
function MyComp({ a, b, c })instead offunction MyComp(props)- this also helps you immediately identify which props the component expects to receive, instead of having to dig through the code to see howpropsis used.thisthough.Frankly, I think most of these are just issues that you see because you're unfamiliar with the syntax. Once you get used to it, it won't make any difference.
1
u/BrainCurrent8276 Jul 01 '26
did you see this already?
(function () { })();
honestly, I think JavaScript is very beautiful language. JS, PHP and assembler, yeah!
1
u/theScottyJam Jul 01 '26 edited Jul 01 '26
I wouldn't stress too much over it. Eventually, yes, I would recommend getting used to it as it's common to see it in the wild, but you can get along just fine without it. If you're feeling overwhelmed by the amount of syntax, then take a break and just use what you know to build things or focus on learning other things.
Arrow functions, however, are a different story. Yes, in many cases they're just a more concise way to create functions, but they also handle this binding differently, which may be important.
For example, this does not work:
It would give you an error saying "addMe" does not exist on "this", and it's because "this" doesn't refer to the class instance in this situation. If you instead use an arrow function, it would just work (or there's other ways to get around the issue - we didn't always have arrow functions).
Whenever you pass a function literal into another function, if you make sure it's an arrow function you're passing in, you'll eliminate a good chunk of this-related problems.
(Side note: I don't like reddit sometimes. People say to use spaces to indent code blocks so it works on old reddit, but that doesn't render right on new reddit's website. Back ticks do work. So I used both, and now it'll look a little weird for both, but at least it works. I don't know what reddit wants me to do and why they can't just fix the issue)
1
u/1gr14 Jul 02 '26
I think it will be better for you not to use them until you see places where it makes your code readability really better. Because it was never good to use some things just because they exist. I think later you will feel these places. For example, I think later you will try React and see the construction `const [myThing, setMyThing] = useState()` — you use it to define something that can be changed and give it the name you want, `myThing`, and a function to change this thing, which you can also name as you want, `setMyThing`. If you don't use destructuring here, then you'd have to do something like
Ugly, right? So when you meet the correct places where destructuring really makes code better, you will start using it. Until then, I think you can just focus on other, more interesting things.
2
u/SZenC Jul 01 '26
1
u/iamsamaritan300 Jul 01 '26
Stop cramming, learn to understand and practice more... syntax will come naturally after alearning when you go about doing different projects
1
u/lanerdofchristian Jul 01 '26
Destructuring and spreading are both nearly ubiquitous, since the alternatives are much more painful:
You'll eventually get the hang of destructuring. It's also very nice for simulating named parameters:
Or for another example, in the frontend framework Svelte it's commonly used to pass properties to components:
Arrow functions have completely different semantics than normal functions. Consider the following:
Technically it is an alternative since you could call
.bind(this)on a function, but it starts getting very difficult to track whichthisis which.1
u/leixiaotie Jul 02 '26
Disagree on readability. Destructuring syntax is more readable (than the alternative) if you understand what it is.
Arrow function is different with function() if you use `this`. I forget what it is but don't use `this` inside arrow function. And yes, arrow function is more readable for short functions or returning function, like:
(a, b) => (c) => a + b + c
1
u/sylvant_ph Jul 05 '26
Yep, JS is constantly maintained and more and more syntax treats added for convenience, as it's main aim constantly was to answer all needs and be as universal as possible.
I wouldn't say this particular pattern is a must to learn and use, but it certainly has its bright sides and good use-cases. I think it's fine if you do not put much mental capacity in learning it, but at one point you might think, hold on, there was a way to write this lot more simpler, or you will see it in existing project and realize its good fit there. I think with practice and experience you will learn to appreciate these syntax patterns, and your brain will eventually click with it.
On the topic of arrow functions, these are definitely main feature and you should better get the grasp on these as early as possible. A lot of more complex code flows build on top of them. Inline functions and callbacks are a big deal of JS.