Moving from Python to Javascript (part 1: What I am loving)

Little intro

In this short (as in 2 parts) series I am exploring what I have discovered that I love and hate (spoiler! that is part 2) of moving from Python to Javascript as a developer.

This is going to be a very opinionated article so I’ll start with a little bit about me as it relates to this opinions. First, I am a very BE oriented developer. For the past 10 years I’ve been working on Python tech stacks in early stage startups (well, neither were so early stage by the time I left). And now, at Boxhub, I’m doing a big paradigm shift into doing the BE in Javascript. Actually, an even more specific technology. We are using Typescript. And even more specific. We are using Next.js. This means that everything you see here will apply to that specific stack and might not apply to barebones Javascript or FE javascript at all.

With all that out f the way let’s start with what I’ve been loving so far.

Types!

This is due to Typescript, and I was already shifting towards the optional typing in Python before I changed gears, but I am loving being strictly typed again (used to be a big C fan what seems ages ago). I personally think forcing the types can help you make way more concise and clear code. Even though I do love the any type. It is always good to have a fallback, especially for quick prototyping were you are still not sure how things are going to connect and you just want to test out some ideas.

Destructuring assignment

Some of it can be done in Python. But basically being able to do this:

const {
  body: {
    this_param: thisParam,
    this_other_param: thisOtherParam,
    complex_composed_param: {
      might_be_missing: missing,
      can_be_defaulted: defaulted = 42,
      sub_param: subParam,
      list_of_things: [
        one,
        two,
        ...rest
      ]
    }
  }
} = myInput;

And just have your correct variables set… that is simply beautiful.

Optional Chaining (.?)

This is another beautiful language construct.

const myInput = {
  param1: "hey",
  param2: {
    inner1: "ho",
    inner2: "let's go"
  }
};
console.log(myInput.bandName); // this gives a undefined (no bandName in myInput) instead of "The Ramones"
console.log(myInput.param2.inner3); // also undefined (param2 is there but no inner3 in param2) instead of "nana na nana"
console.log(myInput.bandMembers.drummer); // this breaks horribly trying to get drummer from undefined bandMembers [basic dot notation (.)]
console.log(myInput.bandMembers.?drummer): // this instead gives a less destructive undefined thanks to the optional chaining [(.) << (.?)]

Ternary Operator

Not much to say there is also one in Python but it is soooo ugly. A if Condition else B … it reads well if you plan on reading your code out loud. If not the classic Condition ? A : B reads way better.

Final Disclaimer

I’ll probably find (and remember) more things I am loving about Javascript. So I lied at the start… this will probably turn into a longer series instead of two long boring posts.

Part 2

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *