Learning JavaScript as an experienced Java developer. Where to start?

Sbodd

Ars Tribunus Militum
1,847
Hello all! Longtime GESC lurker here with a request. I'm a longtime Java UI dev tasked with creating a JavaScript replacement for our SWT
UI. One catch: I can spell JS, but that's about it. Anyone have recommended reading/tutorials for some or all of...

*JavaScript 101 for an experienced dev?
*How to architect complex UIs in JS?
*An overview of useful libraries?
*3D rendering?

Thanks!
 

Lt_Storm

Ars Tribunus Angusticlavius
21,343
Subscriptor++
Hello all! Longtime GESC lurker here with a request. I'm a longtime Java UI dev tasked with creating a JavaScript replacement for our SWT
UI. One catch: I can spell JS, but that's about it. Anyone have recommended reading/tutorials for some or all of...

*JavaScript 101 for an experienced dev?
*How to architect complex UIs in JS?
*An overview of useful libraries?
*3D rendering?

Thanks!

recommended reading: JavaScript the Good Parts. Something on ES6 because ES6 is good. Maybe a book on CoffeeScript (CoffeeScript: Accelerated JavaScript Development, Second Edition is good) or TypeScript, even if you aren't going to use CoffeeScript or TypeScript). Something on HTML / CSS (You should at least understand CSS specificity).

As for UI, that is done with HTML. You might or might not build a single page application; consider React, vuw, or Angular, (and use React) though there are many many other good options. As for libraries: moment.js for date and time processing, though it has a newish competitor; gulp or perhaps Grunt is your build tool; Mocha is unit tests but Jest is new, shiny, zero-conf, and React-friendly; D3 is for data visualization; JQuery for DOM manipulation (but you have React); As for 3d, that depends on your goals.
 

rabish12

Ars Legatus Legionis
16,983
It's been way too long since I learned JS to have a good perspective on what's helpful there, but I will recommend keeping an open mind. The language is designed to look very similar to Java, but a lot of the specifics about how it works are massively different. Closures and first class functions in particular are a huge part of the language, and Java only just recently got decent support for the latter and only kind of supports the former. Also, be willing to look through MDN's documentation whenever you aren't sure how something works or whenever you need a tool for something and don't know if it exists. Javascript's more expressive and powerful than a lot of people realize, but it will bite your face off if you aren't careful.

For building complex UIs, it really depends on the requirements. If it's a public-facing tool, the ideal is to try and limit yourself to HTML and CSS as much as possible and use JS for progressive enhancements (ie. smoother transitions, client-side validation, and so on). That would mean building something that works without any libraries, which is... not easy, especially when you're just learning to developer for the web. For an internal tool or something you need done quicker, you'd be better off using one of the frameworks that Lt_Storm mentioned. The only caveat I'd mention there is that while Angular is probably the easiest to get into, it is pretty badly over-engineered overall and it will expect you to do pretty much everything within the framework, which sucks if you ever decide to migrate away from it.

In terms of libraries, I'd avoid them as much as possible. Moment.js was mentioned and is basically mandatory, because the built-in date support for the language is... flaky. Lodash is pretty standard as an extremely small, minimal set of utility functions. I'd actually avoid JQuery as much as humanly possible - it's bigger, heavier, and less efficient than Lodash, its most popular functionality is largely available natively in JS, and in general it appears to be on its way out.

If you're using React then you're going to need a build tool, and the standard tool for that is Webpack. Gulp and Grunt are useful task runners if you have a complex build that requires something to manage the steps, but when we're talking about web development you really don't want to have a build like that. The more complex your build, the harder it will be to manage and the harder your code will be to debug.

If you need to do 3D rendering, you're probably going to be dealing with either three.js or something built on top of it. I haven't played around much with 3D rendering in JS though, so I can't really help you beyond that.
 

Lt_Storm

Ars Tribunus Angusticlavius
21,343
Subscriptor++
It's been way too long since I learned JS to have a good perspective on what's helpful there, but I will recommend keeping an open mind. The language is designed to look very similar to Java, but a lot of the specifics about how it works are massively different. Closures and first class functions in particular are a huge part of the language, and Java only just recently got decent support for the latter and only kind of supports the former. Also, be willing to look through MDN's documentation whenever you aren't sure how something works or whenever you need a tool for something and don't know if it exists. Javascript's more expressive and powerful than a lot of people realize, but it will bite your face off if you aren't careful.

For building complex UIs, it really depends on the requirements. If it's a public-facing tool, the ideal is to try and limit yourself to HTML and CSS as much as possible and use JS for progressive enhancements (ie. smoother transitions, client-side validation, and so on). That would mean building something that works without any libraries, which is... not easy, especially when you're just learning to developer for the web. For an internal tool or something you need done quicker, you'd be better off using one of the frameworks that Lt_Storm mentioned. The only caveat I'd mention there is that while Angular is probably the easiest to get into, it is pretty badly over-engineered overall and it will expect you to do pretty much everything within the framework, which sucks if you ever decide to migrate away from it.

You can use such frameworks even with a progressive enhancement philosophy. It just takes more work, (as they don't ship out of the box with server side rendering enabled). You just have to make it a point to prerender everything.
 

rabish12

Ars Legatus Legionis
16,983
It's been way too long since I learned JS to have a good perspective on what's helpful there, but I will recommend keeping an open mind. The language is designed to look very similar to Java, but a lot of the specifics about how it works are massively different. Closures and first class functions in particular are a huge part of the language, and Java only just recently got decent support for the latter and only kind of supports the former. Also, be willing to look through MDN's documentation whenever you aren't sure how something works or whenever you need a tool for something and don't know if it exists. Javascript's more expressive and powerful than a lot of people realize, but it will bite your face off if you aren't careful.

For building complex UIs, it really depends on the requirements. If it's a public-facing tool, the ideal is to try and limit yourself to HTML and CSS as much as possible and use JS for progressive enhancements (ie. smoother transitions, client-side validation, and so on). That would mean building something that works without any libraries, which is... not easy, especially when you're just learning to developer for the web. For an internal tool or something you need done quicker, you'd be better off using one of the frameworks that Lt_Storm mentioned. The only caveat I'd mention there is that while Angular is probably the easiest to get into, it is pretty badly over-engineered overall and it will expect you to do pretty much everything within the framework, which sucks if you ever decide to migrate away from it.

You can use such frameworks even with a progressive enhancement philosophy. It just takes more work, (as they don't ship out of the box with server side rendering enabled). You just have to make it a point to prerender everything.
You can, sure, but I don't think I can recommend this kind of setup for someone's first project. I'd honestly recommend against using any of those frameworks at all if it were a hobby project - learning the frameworks with the platform instead of learning the platform first tends to leave people with some big holes in their understanding - but from the sound of it, this is a professional job so that's probably not viable.

I just think it's important to keep things as simple as feasibly possible when you're starting out with stuff like this.
 

Lt_Storm

Ars Tribunus Angusticlavius
21,343
Subscriptor++
It's been way too long since I learned JS to have a good perspective on what's helpful there, but I will recommend keeping an open mind. The language is designed to look very similar to Java, but a lot of the specifics about how it works are massively different. Closures and first class functions in particular are a huge part of the language, and Java only just recently got decent support for the latter and only kind of supports the former. Also, be willing to look through MDN's documentation whenever you aren't sure how something works or whenever you need a tool for something and don't know if it exists. Javascript's more expressive and powerful than a lot of people realize, but it will bite your face off if you aren't careful.

For building complex UIs, it really depends on the requirements. If it's a public-facing tool, the ideal is to try and limit yourself to HTML and CSS as much as possible and use JS for progressive enhancements (ie. smoother transitions, client-side validation, and so on). That would mean building something that works without any libraries, which is... not easy, especially when you're just learning to developer for the web. For an internal tool or something you need done quicker, you'd be better off using one of the frameworks that Lt_Storm mentioned. The only caveat I'd mention there is that while Angular is probably the easiest to get into, it is pretty badly over-engineered overall and it will expect you to do pretty much everything within the framework, which sucks if you ever decide to migrate away from it.

You can use such frameworks even with a progressive enhancement philosophy. It just takes more work, (as they don't ship out of the box with server side rendering enabled). You just have to make it a point to prerender everything.
You can, sure, but I don't think I can recommend this kind of setup for someone's first project. I'd honestly recommend against using any of those frameworks at all if it were a hobby project - learning the frameworks with the platform instead of learning the platform first tends to leave people with some big holes in their understanding - but from the sound of it, this is a professional job so that's probably not viable.

I just think it's important to keep things as simple as feasibly possible when you're starting out with stuff like this.

True, he will need a good grounding in HTML and CSS (especially CSS, it is something that developers tend not to use well), but, overall, I think that React is probably his best bet. The data binding model which it uses is significantly superior to direct DOM manipulation. It makes the system more testable and more reusable. I cannot think of a reason not to use it, even for a beginner, besides, perhaps, licencing (a problem which Facebook fixed a couple years ago).
 

rabish12

Ars Legatus Legionis
16,983
It's been way too long since I learned JS to have a good perspective on what's helpful there, but I will recommend keeping an open mind. The language is designed to look very similar to Java, but a lot of the specifics about how it works are massively different. Closures and first class functions in particular are a huge part of the language, and Java only just recently got decent support for the latter and only kind of supports the former. Also, be willing to look through MDN's documentation whenever you aren't sure how something works or whenever you need a tool for something and don't know if it exists. Javascript's more expressive and powerful than a lot of people realize, but it will bite your face off if you aren't careful.

For building complex UIs, it really depends on the requirements. If it's a public-facing tool, the ideal is to try and limit yourself to HTML and CSS as much as possible and use JS for progressive enhancements (ie. smoother transitions, client-side validation, and so on). That would mean building something that works without any libraries, which is... not easy, especially when you're just learning to developer for the web. For an internal tool or something you need done quicker, you'd be better off using one of the frameworks that Lt_Storm mentioned. The only caveat I'd mention there is that while Angular is probably the easiest to get into, it is pretty badly over-engineered overall and it will expect you to do pretty much everything within the framework, which sucks if you ever decide to migrate away from it.

You can use such frameworks even with a progressive enhancement philosophy. It just takes more work, (as they don't ship out of the box with server side rendering enabled). You just have to make it a point to prerender everything.
You can, sure, but I don't think I can recommend this kind of setup for someone's first project. I'd honestly recommend against using any of those frameworks at all if it were a hobby project - learning the frameworks with the platform instead of learning the platform first tends to leave people with some big holes in their understanding - but from the sound of it, this is a professional job so that's probably not viable.

I just think it's important to keep things as simple as feasibly possible when you're starting out with stuff like this.

True, he will need a good grounding in HTML and CSS (especially CSS, it is something that developers tend not to use well), but, overall, I think that React is probably his best bet. The data binding model which it uses is significantly superior to direct DOM manipulation. It makes the system more testable and more reusable. I cannot think of a reason not to use it, even for a beginner, besides, perhaps, licencing (a problem which Facebook fixed a couple years ago).
I don't recommend it for beginners because learning a scripting language, a markup language, a style sheet language, and a front-end framework at the same time is a huge cognitive load. Even beyond that, React abstracts away some pretty critical concepts in a way that's useful for producing usable projects but not useful for learning - learning React first makes React itself seem like magic, and that's a problem. I still have to recommend it for practical projects - the other options are either not as popular (read: tooling and support suck) or else their design is significantly worse - but if the actual goal is learning, I'm pretty firmly against it.

All that said, one-way data binding isn't unique to React, and neither are any of the other things that make it testable and reusable. I think that all of those things are great, but they're great less because of React itself and more because they enforce good, maintainable coding practices. The distinction's important, because I've known developers who never really learned those principles outside the context of React and the result was always that they wrote trash whenever they weren't forced to avoid it.

tl;dr version: React's probably the best choice for a development framework and I can't recommend doing practical development without it, but if the goal is to learn web development then I think it does more harm than good to start there. If you have to make a practical application as your first web development project, I have to recommend React but I also strongly recommend doing your best to understand how React actually works and why it's designed the way that it is.
 

Lt_Storm

Ars Tribunus Angusticlavius
21,343
Subscriptor++
It's been way too long since I learned JS to have a good perspective on what's helpful there, but I will recommend keeping an open mind. The language is designed to look very similar to Java, but a lot of the specifics about how it works are massively different. Closures and first class functions in particular are a huge part of the language, and Java only just recently got decent support for the latter and only kind of supports the former. Also, be willing to look through MDN's documentation whenever you aren't sure how something works or whenever you need a tool for something and don't know if it exists. Javascript's more expressive and powerful than a lot of people realize, but it will bite your face off if you aren't careful.

For building complex UIs, it really depends on the requirements. If it's a public-facing tool, the ideal is to try and limit yourself to HTML and CSS as much as possible and use JS for progressive enhancements (ie. smoother transitions, client-side validation, and so on). That would mean building something that works without any libraries, which is... not easy, especially when you're just learning to developer for the web. For an internal tool or something you need done quicker, you'd be better off using one of the frameworks that Lt_Storm mentioned. The only caveat I'd mention there is that while Angular is probably the easiest to get into, it is pretty badly over-engineered overall and it will expect you to do pretty much everything within the framework, which sucks if you ever decide to migrate away from it.

You can use such frameworks even with a progressive enhancement philosophy. It just takes more work, (as they don't ship out of the box with server side rendering enabled). You just have to make it a point to prerender everything.
You can, sure, but I don't think I can recommend this kind of setup for someone's first project. I'd honestly recommend against using any of those frameworks at all if it were a hobby project - learning the frameworks with the platform instead of learning the platform first tends to leave people with some big holes in their understanding - but from the sound of it, this is a professional job so that's probably not viable.

I just think it's important to keep things as simple as feasibly possible when you're starting out with stuff like this.

True, he will need a good grounding in HTML and CSS (especially CSS, it is something that developers tend not to use well), but, overall, I think that React is probably his best bet. The data binding model which it uses is significantly superior to direct DOM manipulation. It makes the system more testable and more reusable. I cannot think of a reason not to use it, even for a beginner, besides, perhaps, licencing (a problem which Facebook fixed a couple years ago).
I don't recommend it for beginners because learning a scripting language, a markup language, a style sheet language, and a front-end framework at the same time is a huge cognitive load. Even beyond that, React abstracts away some pretty critical concepts in a way that's useful for producing usable projects but not useful for learning - learning React first makes React itself seem like magic, and that's a problem. I still have to recommend it for practical projects - the other options are either not as popular (read: tooling and support suck) or else their design is significantly worse - but if the actual goal is learning, I'm pretty firmly against it.

All that said, one-way data binding isn't unique to React, and neither are any of the other things that make it testable and reusable. I think that all of those things are great, but they're great less because of React itself and more because they enforce good, maintainable coding practices. The distinction's important, because I've known developers who never really learned those principles outside the context of React and the result was always that they wrote trash whenever they weren't forced to avoid it.

tl;dr version: React's probably the best choice for a development framework and I can't recommend doing practical development without it, but if the goal is to learn web development then I think it does more harm than good to start there. If you have to make a practical application as your first web development project, I have to recommend React but I also strongly recommend doing your best to understand how React actually works and why it's designed the way that it is.

That is a fair position. Really, for learning the language, I would go with JavaScript the good parts and the CoffeeScript book I mentioned earlier (It is really quite a good intro even for JavaScript because it focuses on the translation from CoffeeScript to JavaScript). Then, I would spend some time reading up on the new things in ES6, and finish off with a primer on CSS Selectors.

After that, I would look into picking and understanding the tools you are actually going to use to build the thing you are interested in building. That probably means React, some testing framework (say Jest), a build tool (webpack), and maybe a few use case specific libraries like moment.js or underscore. Then I would add in every linter I could find.
 

Ardax

Ars Legatus Legionis
19,794
Subscriptor
I'm a longtime Java UI dev tasked with creating a JavaScript replacement for our SWT
Do you know why this is happening?

That said, rabish's advice is spot-on. Minimize library usage until you're more familiar with the language. Except for date handling. Get a library for that.

Moment.js was mentioned and is basically mandatory, because the built-in date support for the language is... flaky.
That's being very generous.
 

rabish12

Ars Legatus Legionis
16,983
I'm a longtime Java UI dev tasked with creating a JavaScript replacement for our SWT
Do you know why this is happening?

That said, rabish's advice is spot-on. Minimize library usage until you're more familiar with the language. Except for date handling. Get a library for that.

Moment.js was mentioned and is basically mandatory, because the built-in date support for the language is... flaky.
That's being very generous.
Nah. There are few things worse for software than being flaky, and Javascript's dates are flaky in every sense of the word. They're inconsistent between current and old dates, they're inconsistent between ECMAScript versions, and they're inconsistent between browsers. The only way it could be worse is if it used Math.random to decide what to display.
 

Sbodd

Ars Tribunus Militum
1,847
Thanks for the advice, all! My copy of The Good Parts just arrived, time to start working through it.

I'm a longtime Java UI dev tasked with creating a JavaScript replacement for our SWT
Do you know why this is happening?

There's several reasons, some of them good. The only really stupid part is not including someone who's done an at-scale web UI on the team. Honestly, I'm looking forward to adding this to my skillset, just wish I had more time to learn or a decent mentor available.
 

Horatio

Ars Legatus Legionis
24,430
Moderator
'm a longtime Java UI dev tasked with creating a JavaScript replacement for our SWT
Does the new implementation have to be JavaScript per se, or could you use a language to compile to JavaScript? These days I'd say most large scale JS development is being done in one of the languages that compiles to JS, rather than raw JS. TypeScript is probably the most popular at this time, but others like CoffeeScript have been mentioned. It's probably a good idea to get a strong handle on JS in general first, but I wouldn't write a significant implementation in raw JS these days. Even holdouts like Airbnb and FB are switching to TS now.
 

Lt_Storm

Ars Tribunus Angusticlavius
21,343
Subscriptor++
'm a longtime Java UI dev tasked with creating a JavaScript replacement for our SWT
Does the new implementation have to be JavaScript per se, or could you use a language to compile to JavaScript? These days I'd say most large scale JS development is being done in one of the languages that compiles to JS, rather than raw JS. TypeScript is probably the most popular at this time, but others like CoffeeScript have been mentioned. It's probably a good idea to get a strong handle on JS in general first, but I wouldn't write a significant implementation in raw JS these days. Even holdouts like Airbnb and FB are switching to TS now.
The real big problem with that is that TS and especially CoffeeScript aren't really more like extensions to JavaScript than entirely different languages. You still need to understand JS. Oh, and with ES 6, and gradual typing approaches you can make it really really far with just JS these days (though, given browser support, you still might need to compile your JavaScript to JavaScript).
 

Horatio

Ars Legatus Legionis
24,430
Moderator
The real big problem with that is that TS and especially CoffeeScript aren't really more like extensions to JavaScript than entirely different languages. You still need to understand JS.
Right, which is why I said to get a handle on JS in general first. Still, even in ES6 and beyond, it's miles easier to write in TS (for me at least).
you still might need to compile your JavaScript to JavaScript
This is another area where TS shines, since it can compile down to many versions of JS, but you can always write as if you were using the latest ES version and it'll polyfill the missing bits.
 

Lt_Storm

Ars Tribunus Angusticlavius
21,343
Subscriptor++
The real big problem with that is that TS and especially CoffeeScript aren't really more like extensions to JavaScript than entirely different languages. You still need to understand JS.
Right, which is why I said to get a handle on JS in general first. Still, even in ES6 and beyond, it's miles easier to write in TS (for me at least).
you still might need to compile your JavaScript to JavaScript
This is another area where TS shines, since it can compile down to many versions of JS, but you can always write as if you were using the latest ES version and it'll polyfill the missing bits.
ES6 does that too, you just use Babble. The big question is: how much stock do you put in compile-time type checking. I don't necessarily find it super useful, but YMMV.
 

Horatio

Ars Legatus Legionis
24,430
Moderator
ES6 does that too, you just use Babble. The big question is: how much stock do you put in compile-time type checking. I don't necessarily find it super useful, but YMMV.
Yeah, you can use Babel, but TS seems easier (for me). I get a lot of value from the type checking personally, and it's prevented a lot of bugs IME.
 

Lt_Storm

Ars Tribunus Angusticlavius
21,343
Subscriptor++
ES6 does that too, you just use Babble. The big question is: how much stock do you put in compile-time type checking. I don't necessarily find it super useful, but YMMV.
Yeah, you can use Babel, but TS seems easier (for me). I get a lot of value from the type checking personally, and it's prevented a lot of bugs IME.

Part of the reason why I don't put much stock in type checking has to do with my general approach to development and testing. I think that, since type checking only detects a specific class of bug, you still have to write tests anyways. If you do enough testing to reliably detect non-type related bugs, then your tests are likely to give you most of type related bug coverage that a type system like Java's will give you. This implies that, with compile time type checking, you probably still have to right the same tests you would without it; and, if you are doing your job well, type checking mostly involves a sacrifice of flexibility with little related gain.

That said, something which propagates types really effectively like Haskell could be a very interesting compromise between flexibility and provable correctness.
 

rabish12

Ars Legatus Legionis
16,983
ES6 does that too, you just use Babble. The big question is: how much stock do you put in compile-time type checking. I don't necessarily find it super useful, but YMMV.
Yeah, you can use Babel, but TS seems easier (for me). I get a lot of value from the type checking personally, and it's prevented a lot of bugs IME.

Part of the reason why I don't put much stock in type checking has to do with my general approach to development and testing. I think that, since type checking only detects a specific class of bug, you still have to write tests anyways. If you do enough testing to reliably detect non-type related bugs, then your tests are likely to give you most of type related bug coverage that a type system like Java's will give you. This implies that, with compile time type checking, you probably still have to right the same tests you would without it; and, if you are doing your job well, type checking mostly involves a sacrifice of flexibility with little related gain.

That said, something which propagates types really effectively like Haskell could be a very interesting compromise between flexibility and provable correctness.
I think the main issue a lot of people have with type-checking is just that so many languages' type systems are... well, kind of awful. Like, Java's type system both fails to prevent a lot of potential bugs (including type-related bugs), but also forces you to be ridiculously explicit about even the most obvious of type information. It's a huge overhead for veeeery little gain.

I think the type system for Typescript is far, far more reasonable, particularly with stuff like strict null checking turned on. It does a decent job of inference, the decisions they made with it actually make sense, and I rarely find myself wrestling with the type system when I feel like I should be wrestling with the code.

EDIT: Testing is fine, but not having to write tests for nulls, undefined values, malformed values, and a whole bunch of other possibilities, as well as not having to account for those cases in your code, is really valuable. You can't get bugs from that stuff if that stuff can't happen.
 

Lt_Storm

Ars Tribunus Angusticlavius
21,343
Subscriptor++
ES6 does that too, you just use Babble. The big question is: how much stock do you put in compile-time type checking. I don't necessarily find it super useful, but YMMV.
Yeah, you can use Babel, but TS seems easier (for me). I get a lot of value from the type checking personally, and it's prevented a lot of bugs IME.

Part of the reason why I don't put much stock in type checking has to do with my general approach to development and testing. I think that, since type checking only detects a specific class of bug, you still have to write tests anyways. If you do enough testing to reliably detect non-type related bugs, then your tests are likely to give you most of type related bug coverage that a type system like Java's will give you. This implies that, with compile time type checking, you probably still have to right the same tests you would without it; and, if you are doing your job well, type checking mostly involves a sacrifice of flexibility with little related gain.

That said, something which propagates types really effectively like Haskell could be a very interesting compromise between flexibility and provable correctness.
I think the main issue a lot of people have with type-checking is just that so many languages' type systems are... well, kind of awful. Like, Java's type system both fails to prevent a lot of potential bugs (including type-related bugs), but also forces you to be ridiculously explicit about even the most obvious of type information. It's a huge overhead for veeeery little gain.

I think the type system for Typescript is far, far more reasonable, particularly with stuff like strict null checking turned on. It does a decent job of inference, the decisions they made with it actually make sense, and I rarely find myself wrestling with the type system when I feel like I should be wrestling with the code.

EDIT: Testing is fine, but not having to write tests for nulls, undefined values, malformed values, and a whole bunch of other possibilities, as well as not having to account for those cases in your code, is really valuable. You can't get bugs from that stuff if that stuff can't happen.

I can certainly see some benefit to especially not having to write null checks, but would be more inclined to handle that through the marco system (I am a Smalltalker / Lisper at heart) or using a linter. But the problems still occur when, for example, the libraries you use have two different root classes for the streaming interface. Hey, now you can't chain maps and reduces across them. Fun.

Which is the real point: there are multiple ways of handling such issues, gradual typing is a thing, and strict type checking at compile time doesn't generally give me what I want. (Though, as I said, ML and Haskell have interesting solutions that may give most of the benefit of both worlds).

Edit: oh, and you still have to write tests for at least malformed values, and likely you also need to include runtime null checks at the top level interface of your libraries so that when someone calls it from JavaScript it doesn't explode unusually (unless Typescript adds that in for you).