Sometimes reading comments about what microcontrollers aren't able to do in 2020 looks like cargo cult of people lacking the experience of how it was like to code with 640 KB.
function increment() {
var out = []
for (let i of [1,2,3,4,5]) {
out.push(i + 1)
}
return out
}
vs ES6: const increment = () => [1,2,3,4,5].map(i => i + 1)
etc. Most people's experience with JS/TS has simply been before ES6 (which had massive problems, like `var` and `this` scoping etc.), it's really nice nowadays. var increment = [1, 2, 3, 4, 5].map( function(num){ return num + 1 } )
The two big syntactic wins of modern JS are let/const over the filth that was var and the convenience of arrow functions over anonymous function literals. var increment = function() {
return [1,2,3,4,5].map(function(num) {
return num + 1;
});
};
Moreover, modern JS is not merely convenient syntactic sugar over old JS. Let/const have different scopes, arrow functions have no bindable context.You also now have classes, proxies, async/await, generators, destructuring, default/rest/spread parameters, ...
Add typing via jsdocs or TypeScript and - like the parent comment said - "it's really nice".
Often people mistake JS for a mere DOM manipulation language and think of
document.getElementById('myDiv').innerHTML = 'Hello World';
instead of what it has become today.I still hate it though for sorting arrays with only number types lexically:
[1,2,10,20].sort(); //[1,10,2,20]I will concede that I failed to write a function definition, thanks for catching that. I guess I unintentionally refactored the outer function away because development brain took over? Sloppy on my part, regardless.
The point was that modern JS is more convenient to use: Const/let, arrows, classes, proxies, async/await, generators, destructuring, default/rest/spread parameters, etc.
Even if you discard const/let/arrows as merely syntactic improvements (which they are not), you should not ignore the others - unless of course they are all syntactic improvements with added features at which point "syntax" lost its "meaning".
In fact, your example suffers from the same problem: It sorts in descending order (20,10,2,1) instead of ascending (1,2,10,20).
Telling JS to use a TypedArray [Int8,...,BigInt64] instead can solve this without remembering and providing your own comparator:
Float64Array.from([1,2,10,20]).sort(); //[1,2,10,20]I.e. due to the lack of typing this will fly just fine:
const calculateInterestingNumber = () <calculations goes here>;
let twiceAsInterestingNumber = 2 * calculateInterestingNumber; // (ooops, missing parenthesis)
... until runtime.
This was just the silliest contrived example.
In practice this - as you are fixing someone else's code and misplace/forget a set of parenthesis - ends up taking 30 minutes to figure out.
I highlight that. In fact, Rust is another kid on the block that is very promising for embedding.
In the other hand, sorry but I simply don't get why JS for interactions on such highly constrained devices. Probably as you said, it's intended to do very simple things with less effort as possible.
Compiling expressive syntax through a powerful optimizing compiler to produce binaries run by microcontrollers is really sweet. Definitely the way to go, if you want to get the most functionality from limited hardware, without going all the way to assembly.
I guess JavaScript could be good for people who want to do very simple things, and then invest lots of time fighting limitations as their ambitions grow, rather than learning something useful. Like people do with Excel.
Which brings me to my next question: why not take it to the limit, and make microcontrollers programmable purely in Excel macros? That would be maximally accessible. Lots of people know Excel and can’t get enough pain.