Javascript is my favorite language

Table Of Contents ↓

…well, one of!

Javascript has a bad wat reputation. Despite the difficult history and being an ugly-duck of programming languages there are some ideas that Javascript today does better than other languages in my opinion.

Brendan Eich wrote javascript in 10 days, in May 1995 https://en.wikipedia.org/wiki/JavaScript

Personal context

In past I’ve never fully commited to writing Javascript. jQuery was my goto to enhance Rails apps for a long time but I never really invested into learning the language.

It was like Bash scripting: most people know how to use it but never really learn how to program with it beyond simple helper scripts.

With NodeJS things did change and I worked on projects where I started noticing features other languages don’t have.

I like learning languages that teach me something new, with Javascript one can say it’s prototype based programming. But that’s not what I like the most.

Javascript, the rebel

What I appreciate about Javascript is that it provides its own perspective.

Ie, Javascript had own solutions to problems most OOP languages solve with classes:

If you have state – use classes

or

OOP model familiar to everyone and easy to use

Unfortunately, the peer pressure was so strong that Javascript got class in 2015

JavaScript classes, introduced in ECMAScript 2015, are primarily syntactical sugar over JavaScript’s existing prototype-based inheritance. The class syntax does not introduce a new object-oriented inheritance model to JavaScript.

I’m convinced there’s no need for class in Javascript. Strong argument against classes is React’s transition to functional Hooks

For a while, the canonical answer has been that classes provide access to more features (like state). With Hooks, that’s not true anymore. How Are Function Components Different from Classes?

Now why is it all important?

Well, what if there are better ways to program and not use any classes?

Javascript, my favorite parts

  1. State with functions
  2. Named arguments, named returns
  3. Decomposition: Destructuring and Spreads

1. State with functions

Returning a closure from a function enables “having state”:

// https://codepen.io/gmarik/pen/pXPxMR
const Count01 = (start) => {
  let count = start
  return () => {
    count += 1
    return count
  }
}
const inc = Count01(4)
console.log(inc())
// => 5

2. Named arguments, named returns

This approach is beautifully explicit: behavior is defined exactly by what’s returned by the function(aka constructor, compared to OOP’s opaque constructor)

// https://codepen.io/gmarik/pen/Prmxqw
const Count02 = ({start = 1}) => {
  let count = start
  const get = () => count
  const inc = () => count += 1 
  return {get, inc}
}

const counter = Count02({start: 3})
counter.inc()
counter.inc()
console.log(counter.get())
// => 5

3. Decomposition: Destructuring and Spreads

Destructuring is a duck typing in a way: it lets client request specific functionality. Thus focus shifts from types to behavior:

// https://codepen.io/gmarik/pen/OejgqM
const {log, table, ...rest} = console
log("hello world")
table(rest)

Conclusion

Thank you!

for reading this far.

Read More
SwiftUI Hello World
Datadog APM: Traces + Metrics
Comments
read or add one↓