Boxing 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 More

Merry 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 More

Closure

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 More

Literally

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 More

Equal 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 More

More 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 More

Golden 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

SvelteScript

As I’ve been experimenting with Svelte, I wanted to hone my JavaScript skills. I’ve done some development in the past but it’s been a while and things have changed. I did some research to figure out what might be a good resource and ended up reading Modern JavaScript for the Impatient to get back up to speed. As it says on the tin, it’s all about modern JavaScript and skips much of the old and weird stuff that you really don’t need to know in favor of getting to the point for developers who know other languages.

Read More

Svelter - part III

I went through the rest of the Svelte tutorial to understand the basics. It’s a nicely structured tutorial and I was able to get the gist of the major features of Svelte by going through it. Svelte has a lot of power and the compilation process makes it simpler to deal with than some of the other modern frameworks. The proof is in the pudding though and I’ll need to actually build something to see how it all fits together.

Read More

Good news

If all you read are the headlines, you would think that the world is going to Hell in a coal-fired hand basket driven by Vladimir Putin. However, there is some good news out there. Future Crunch has a great article pointing out some of the good stuff that is happening in the world. It’s a much needed jolt of positivity as we enter into the winter darkness. My Top 10 are 1, 11, 12, 39, 41, 49, 69, 70, 82 and 87.

Read More