DEV Community

Gomandev
Gomandev

Posted on

What will you change or add in javascript if you have a chance?

I was asked this question in an interview sitting a couple of months ago. I'm pretty sure some of you really ever thought of changing something in JS language.

I will be happy to know your opinion in the comment.

Discussion (36)

Collapse
lukeshiru profile image
LUKESHIRU

Some things I would like:

  • Fix the typeof null bug would be a good start, but it would be even better to just get rid of it or undefined, so we only have one of them.
  • Getting rid of classes.
  • Rethinking the DOM and Web Components functionally instead of class based.
  • Making Objects behave like Records, and Arrays behave as Tuples.
  • Making == behave as ===, and !== as !==.
  • Getting rid of any automatic type casting.
  • Making arrow functions use the one argument with direct return format only: argument => argument * 2.
  • Making default property access behave as optional chaining does.
  • Make ESM the only module system (no CJS) and while you're at it, change it to be from "location" import { thing }.
  • Get completely rid of globals of any kind (window, globalThis) and make them importable from somewhere.

If I think a while longer I might be able to come up with more stuff, but this comment is long enough already.

Cheers!

Collapse
siddharthshyniben profile image
Siddharth

Only if you did this, almost every site would break.

Collapse
lukeshiru profile image
LUKESHIRU

I know ;_; ... the idea was to just imagine, is not practical at all xD

Thread Thread
siddharthshyniben profile image
Siddharth

I know XD

Collapse
insidewhy profile image
insidewhy

Even in pure functional languages like Haskell, there are things like classes. Sometimes classes even enable more elegant functional designs. Sure people abuse OO horribly, but removing classes from the language entirely is too dogmatic.

Collapse
lukeshiru profile image
LUKESHIRU • Edited

Haskell has "type classes", not actual classes. Classes were used for years as an encapsulation system, but having modules, classes don't make much sense:

export default class {
    #secret = 42;
    constructor({ name = "shape", x, y }) {
        this.name = name;
        this.x = x;
        this.y = y;
    }
    move({ x, y }) {
        this.x = x;
        this.y = y;
    }
}

// and then...

import Shape from "./Shape";

const shape = new Shape({ x: 10, y: 10 });
shape.move({ x: 0, y: 0 });

// vs

const secret = 42;
export const create = ({ name = "shape", x, y }) => ({ name, x, y });
export const move =
    ({ x, y }) =>
    shape => ({ ...shape, x, y });

// and then...

import { create, move } from "./Shape";

const shape = create({ x: 10, y: 10 });
const movedShape = move({ x: 0, y: 0 })(shape);
Enter fullscreen mode Exit fullscreen mode
Thread Thread
insidewhy profile image
insidewhy

Your example doesn't require any encapsulation, the only private data is shared by all objects; and that would definitely be one good usecase of classes.

Thread Thread
lukeshiru profile image
LUKESHIRU • Edited

I didn't used #secret, but that's "private data". You don't need a class to have encapsulation, you can use modules and closures for that nowadays. You export what's "public" and don't export what's "private".

Thread Thread
insidewhy profile image
insidewhy • Edited

No you can't, not when private data has to be encapsulated into a struct like concept, not unless you're willing to sacrifice a great deal of performance and memory.

I think you're being too dogmatic.

Thread Thread
lukeshiru profile image
LUKESHIRU

Encapsulation doesn't necessarily means that you have to have your data in a class. Classes in practice are just functions, so you can use functions and closures instead and the performance is pretty much the same.

You say I'm being dogmatic, but you can't imagine OOP without classes :/

Collapse
auroratide profile image
Timothy Foster

change it to be from "location" import { thing }

Now that you mention it, I never realized how much I wanted this syntax until now.

Collapse
exenestecnico profile image
Ernesto

I cringe reading this. You want to remove mostly optional stuff to make JS more like other language. My guess is you don't want to use JS and really want Python support in browsers.

Collapse
lukeshiru profile image
LUKESHIRU

lol why will you cringe? The idea of the post is to imagine how JS could be better, maybe for you JS will be better if it only had classes, I wouldn't cringe with your opinion ... and Python in browsers? I don't even like Python! 🤣

Instead of cringing over other users lists, why don't you just write your own list of features in a comment, or maybe actually mention what things on my list aren't of your liking? I mean, do you really like for example to have typeof null === "object" or you agree with me about that one?

Collapse
siddharthshyniben profile image
Siddharth

Just a question: Why from ... import? Is there any benefit?

Collapse
lukeshiru profile image
LUKESHIRU

Mainly because for some editors makes auto-complete easier because you first define the path for the import and then the thing you want to import from it.

Collapse
gomandev profile image
Gomandev Author

Thanks for your opinion, man!

Collapse
jonrandy profile image
Jon Randy

You're removing a lot of stuff that makes JS a joy to work with

Collapse
nombrekeff profile image
Keff

I agree, some of them where a bit extreme.

I also agree that some of the things like the null bug and such are a pain in the arse. But I don't think we should remove classes or force arrow function to only accept one argument. I like the freedom JS gives you, though that's also a bad thing about it :P

You might not like classes, but they have a place and can be really usefull. Not eveyone likes going all functional, to that peple I say, it's okay to be wrong xD joking, I do enjoy both

Thread Thread
jonrandy profile image
Jon Randy

Strictly speaking, JS still doesn't have classes. It's just sugar to make it look like it does. Unnecessary really

Thread Thread
lukeshiru profile image
LUKESHIRU • Edited

My main issue is that JS initially didn't had classes. They just added that to attract Java devs (the language itself is called JavaScript to be appealing for Java devs), and I feel they lost an opportunity to keep improving in the functional approach. Still, JS is as popular as it is not only because it comes bundled with browsers, but because it allows you to do things with a OOP approach, or FP, or a mix, and it works.

About the arrow functions, my point mainly is that if you'll write this:

(a, b) => {
  return a + b;
}
Enter fullscreen mode Exit fullscreen mode

You aren't that far away of...

function (a, b) {
  return a + b;
}
Enter fullscreen mode Exit fullscreen mode

But if you keep it simple and add curry to the mix, you get the actual value of arrow functions:

a => b => a + b;
Enter fullscreen mode Exit fullscreen mode

So if you need several arguments and a body, you could just keep using function, and when not you could use an arrow ☺️

Still, these are just the things I would like and will never happen. I'm willing to bet folks will like JS to be even more class heavy, but being a post about opinions, I guess everyone is welcome to imagine the ideal JS for them 😄

Thread Thread
nombrekeff profile image
Keff

Ohh yeah, I'm aware.. I agree that they are kinda unnecesary, but they make my life a bit easier. I fricking hated constructor functions, but now I kinda enjoy working with "classes". I prefer not to if I can, but sometimes I need to

Couldn't we also consider that high-level programming languages are also just sugar?

Thread Thread
nombrekeff profile image
Keff • Edited

One of the things I like about arrow function is that, you don't need to fill your code with the function keyword, reducing the amount of noise and clutter. I really enjoy those kinds of things, where you don't need to write unnecesary keywords. Take Java for example, a big percentage of the code is just public, private, final, static... I don't enjoy that, it's a mess (but they have a place and I can see the value of them).

And although I would not like this change to be implementet, I get your point :P

I'm willing to bet folks will like JS to be even more class heavy

Let's hope it does not go that way, at least not excesively 🤞

I guess everyone is welcome to imagine the ideal JS for them 😄

Of course, that's what I like about this types of posts, they make us talk about this things and respectfully debate them

Collapse
lukeshiru profile image
LUKESHIRU

What of all that makes JS a joy? They are pretty much a list of "JS the bad parts" x'D

Thread Thread
gomandev profile image
Gomandev Author

I agree with you @lukeshiru !

Collapse
kaviisuri profile image
KaviiSuri

DATE!!!
The date and time in javascript is so messed up, the community had to create libraries for basic tasks like date and time management.
Luckily, there is a new API coming called the Temporal API. Fingers Crossed 🤞

Collapse
gomandev profile image
Gomandev Author

Great update!

Collapse
manoharreddyporeddy profile image
Manohar Reddy Poreddy • Edited

I will be much more productive if below are available:

JavaScript

  • Add more data structures
    • stack, queue, heap/ priority_queue, trie, etc from c++/java/python
  • Add more algorithms
    • binary_search, upper_bound, lower_bound, etc from c++/java/python
  • Even shorter syntax constructs where possible
  • Make things faster where possible
    • .forEach is slow most cases

Node.js

  • Start execution from function main() if given, with command line args to it
Collapse
gomandev profile image
Gomandev Author

Great suggestions, man. Having an inbuilt algorithm data structure will be fantastic.

Collapse
hiro98 profile image
Valentin Boettcher

Make it never happen in the first place :P.

Collapse
hiro98 profile image
Valentin Boettcher
Collapse
creativemacmac profile image
creativemacmac

if I had a magic wand I would make the bugs and errors in javacsript more informative so that we knew how to fix them without spending hours and days even :)

Collapse
taufik_nurrohman profile image
Taufik Nurrohman

Rename XMLHttpRequest with XMLHTTPRequest.

Collapse
ivanjeremic profile image
Ivan Jeremic • Edited

Remove classes no one needs them. JS is anyway the most object oriented language even without them.

Collapse
gomandev profile image
Gomandev Author

I am also personally not a fan of classes.

Collapse
barraza44 profile image
Alex Barraza

I would add something like the extension methods found in C#

Collapse
bhumit070 profile image
Bhoomit Ganatra

I would like to remove all those things which are repetative bevause in javascript there is more than one way to perform a single thing and I hate that most