Javascript for Programmers

Javascript is one of the need-to-know programming languages – once much-maligned but now begrudgingly respected as fully matured, worthy of knowing and using.

Latest Versions

The latest versions of Javascript, ECMAScript 6 (or 2015) and ECMAScript 7 (or 2016), offer features that improve Javascript as a modern language. Unfortunately these versions are only partially supported by browsers. However, libraries like Babel come to the rescue by converting your code backwards to fully supported 2011 version. It sounds messy but can be turned into a simple process. I use nodejs with Babel and Visual Studio Code and ESLint to work in Javascript, as I do with C++, Java, and C#.

Two Great New Features

My first choice and that of any others is the new Javascript arrow function. So compare tradition declaration with the newer arrow function below. As well as being much cleaner (less noisy) the arrow functions offer other advantages, especially for functional-style programming. Check out the link above for more benefits. By the way, => is often called a ‘fat arrow’.

Compare new and old style JS functions

This image introduces another great Javascript improvement – const and let for declarations. Seriously don’t use var. const forces you to declare and assign an immutable variable while let (mutable) offers better scoping and helps with the dreaded Javascript hoisting issues. I mention a const caveat later.

Use newer style JS declarations

Want More Than Javascript?

Do you want more than modern Javascript? There are many libraries and applications to help, and most are free and open source. The following projects caught my interest, even though I am not endorsing them. Your choice depends on needs and experience. Just stay clear of projects that depend on one developer who may lose interest. The project can quickly die as many have. Also be wary of those not updated for a few years. Check the GitHub source dates for last updates. Here are two comprehensive lists to start your search:

https://github.com/jashkenas/coffeescript/wiki/list-of-languages-that-compile-to-js
https://www.slant.co/topics/101/~best-languages-that-compile-to-javascript

Javascript With Extras

This is a popular category with offerings like CoffeeScript (and spin-offs) and TypeScript from Microsoft. You still work with Javascript, but they add extra features like type declarations.

JS++ enhanced Javascript

JS++ offers full support for compile time and run time type checking. As well as offering impressive enhancements, JS++ works well with existing libraries and code. I used Ramda, functional programming library, with no hassles. The quality and depth of JS++ seem to address many concerns for developing large scale Javascript applications. Combined with good documentation makes JS++ worth a look.

js++ Integration with File Explorer

Finally, another great feature is its integration with Windows, my development platform, and File Explorer. This is great for quick compile-time checking, even though the command line is still available.

 

So here is an example of conversion. I declared a function and its parameters as Int (integer) types. You notice it is JS 2011 standard that is widely accepted.

 

 

Functional Programming and Ramda

Javascript is not a dedicated functional programming language like Clojure, Haskell, and O’Caml. But Javascript offers features to make functional applications, if you have the desire and discipline. For me functional programming offers simpler, more robust code that is easier to test before deployment. However I suggest using a good Javascript functional library to help. This list is a good starting point.

https://www.slant.co/topics/408/~best-libraries-for-functional-programming-on-node-js

Ramda is a popular Javascript functional library offering simplicity and good documentation. However you need to spend time learning about functional programming and, how it differs from object orientation. There are plenty of on line resources as well many good books, including this one from Apress that helped me, Beginning Functional JavaScript. The author steps through building your own library – a great way for understanding.

Here are two different ways of creating a new list from an existing one. Simply adding one to every value. The imperative way is to loop explicitly through a list with temporary variables. By contrast, in functional programming we code a rule for the map function to complete the process. We do not care how that is done. You will notice the Ramda library is treated as an object and is referenced by R., such as R.map(. A final remark about this code. I could have declared newList as a const, and the code would still work. const blocks you from reassigning a variable name to a new list, in this case. If you want stricter immutability, then check out https://github.com/facebook/immutable-js.

 

From Other Lingos?

Many developers are comfortable with other languages like Python, Ruby, Clojure, and Scala. They want to reuse existing code in Javascript. My earlier links listed many projects of language converters to Javascript for you to explore. Here are some that caught my attention.

Pythonic Takes

Transcrypt uses a Python coding to produce 2011 or 2015 Javascript code. Its aim generate efficient Javascript code with access to the full Javascript ecosystem. So it supports the easy inclusion of existing Javascript code but little support for existing Python libraries. However the project does include part of the NumPy library to use.

Everything about the website, documentation, and software demonstrates that this is a serious effort to bring Python advantages to Javascript. Here is a quote for the Transcrypt website of the Python features you retain.

Multiple inheritance, recursive tuple assignment, multi-loop nested list comprehensions, LHS and RHS extended slices, assignment of bound functions, lambdas, named, default, *args and **kwargs parameters, properties, optional operator overloading, iterators, generators, async/await, selective exception handling and a hierarchical module system are just a few of its characteristics that make this clear. Transcrypt is parsed by CPython’s AST module, so no surprises there.

The good news for debugging is a simple correspondence between the Python code and generate Javascript as the images below show. I took some existing blockchain coding (simplified) and show the generated Javascript (see blow). Incidentally, Transcrypt generates multi-level source maps for debugging.

Python code followed by generate Javascript. Uses Javascript SHA256 library!

 

Lisp, Closure, and Parentheses

Finally let’s look at a project that generates Javascript from a Lisp/Clojure style language, RamdaScript. RamdaScript appears to be a small project that would appeal to Ramda library users. It does to me.

RamdaScript uses a Lisp style coding with about six commands combined with Ramda functions. The software generates clean code that references the Ramda library. You can embed Javascript at any time between #….#.

A really cool thing about RamdaScript in the online compiler at https://yosbelms.github.io/ramdascript/. As you type completed expressions in RamdaScript the equivalent Javascript appears on the right side. This is a great way of learning the basics of a Lisp style language, like Clojure, as well as The Ramda library. Or you can simply paste the generated code into your Javascript application.

 

 

 

 

 

 

Other Javascript Compilers

There are other good compilers for Javascript from popular languages. ClojureJS implements the Clojure language, not surprisingly. RapydScript is an interesting alternative for Python users, while Ruby users can look at Opal. There are many other projects that are also worthy of exploring. Google your language and Javascript to see what links display.

Closing Remarks

Javascript is truly a full member of today’s language pantheon despite quirks that continue. More and more programmers are using Javascript for full-stack development of significant web applications. You can find many well written and supported libraries that show Javascript has come of age. It is no longer a toy scripting programming language to make glossier websites.

Leave a Reply

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