I don’t know why I bother. I’ve been following advances in AI with interest and it’s getting scary good. A big story recently is about how uncannily good ChatGPT is at writing text that is essentially indistiguishable from what a person might write. So, I tried it by generating some text on a subject like yesterday’s post: what is JavaScript spread syntax? The ChatGPT response? JavaScript spread syntax is a way to expand an array or an object into a list of items or key-value pairs, respectively.
Read MoreMore Arguments
Spread syntax is a way of calling a JavaScript object with any number of arguments. So, you can make function like this: const avg = (first = 0, ...others) { let sum = first; for (const value of others) { sum += value; } return sum / (1 + others.lenght); } This can be called with any number of arguments. One thing to note is that if the argument is an array, the result will be NaN.
Read MoreArguments
Arguments are a mere suggestion to call a function in JavaScript. If you supply more arguments than required, they are simply ignored. If you supply less, the missing ones are set to undefined. To handle the case of fewer arguments than the function specified, you can either check for undefined or provide default arguments. Both of these work: const avg = (x, y) => y === undefined ? x : (x + y) / 2; const avg = (x, y = x) => (x + y) / 2; I prefer the latter but your mileage may vary.
Read MoreBoxing Day
The Boxing Day entry is all about testing argument types in functions. JavaScript does not allow you to specify types of function arguments. If you want to know what has been passed into a function, you’ll need to test it. Type Test String typeof x === 'string' || x instanceof String Regex x instanceof RegExp Number typeof x === 'number' || x instanceof Number Convertible to Number typeof +x === 'number' Array Array.
Read MoreMerry Christmas
The Christmas edition is about strict mode in JavaScript. The short story is that you should always use strict mode. The longer version is that strict mode will save your sanity when writing and debugging JavaScript. The essential properties of strict mode are: Assigning a value to an undeclared global variable throws an error. You must use let, const or var to declare a variable before assigning it. You cannot assign a new value to a read-only global value like NaN or undefined.
Read MoreClosure
The Christmas Eve post is about closures. Closures are an interesting property of JavaScript. Basically, it means that you can reference variables in an outer scope from an inner scope. The trivial example is something like this: const sayIt = (text, when) => { let task = () => console.log(text); setTimeout(task, when); } This inner anonymous function has access to the text variable. Basically, this allows a variable to mean exactly the same inside a function as outside.
Read MoreLiterally
An interesting way to think about functions declarations in JavaScript is to treat the anonymous function literal declaration as the baseline case. The named function syntax is a shorthand for declaring a function literal and giving it a name. An anonymous function literal looks like: results = [0, 2, 4, 6].map(function (x) { return 10 * x }); The special case would be to do something like: function applyMagnitude(x) { return 10 * x; } which would then be applied with
Read MoreEqual or not
Yet more JavaScript notes. This time, it’s all about equality. They key part to various control structures (e.g., if, switch, while, do and for)in JavaScript (and other languages for that matter), are the comparison operators. These are less than, greater than, equals and the combinations thereof. In JavaScript, the operands (the things that are being compared) get converted automatically. So, you need to be careful and favor the strict comparisons.
Read MoreMore JavaScript Notes
A couple of winter solstice notes on JavaScript from my recent reading. The first is on automatic semicolon insertion. You don’t have to use semicolons to write JavaScript, but if you don’t, some rules apply. The first rule is pretty simple: when processing a statement, the parser includes everything until it encounters a semicolon or an “offending token” that cannot be part of the statement. If this offending token is a line termination, end of input or a }, a semicolon is added.
Read MoreGolden Rules
As mentioned in my previous post, I’m reading JavaScript for the Impatient. Something I really like about it is that the author, Cay S. Horstmann is opinionated. Technical books need to have a point of view. If you disagree with it, fine. You can take another path but part of the value of reading a technical book is to understand better ways to do it and that is almost always an opinion.
Read More