Bit-wise Operators
Hey you remember how in part 1 of me complaining about JavaScript I was quite annoyed with the language’s disregard for different types of number? Hey guess what I’m still quite annoyed. Because reasons, JS includes operators for doing some of my favorite nonsense, bit-twiddling-hackery. I love me some good bit-twiddlin’, but I ended up getting caught in a weird way by this implementation of my favorite operators.
let num = 1 << 4;
Cool this is all fine. num
is a number and its 16 now no problems here. Everything in JavaScript is a 64 bit floating point value, so bitwise operators don’t even really make a whole lot of sense. To counter this, all bitwise operations in JS are done after converting the values to 32 bit signed integers. After the work is done, it gets converted back. Some people probably are upset about the data loss here but eh we’re in web land who cares. (Probably people writing networking code, who are honestly probably the only ones trying to bitshift in JavaScript)
void(0)
This isn’t something you’d reasonably expect to encounter in code written by a human, but for some reason the language supports it anyway. In most languages void
means “nothing”. You just put it as your return type, your function doesn’t return any value. In C, if your function accepts nothing you should make your function accept void. In JavaScript those things are still true, but void
is also a function. You can call it at runtime. It accepts exactly one argument, and returns undefined
. Any other number of arguments, unlike any other function in JavaScript, will cause a failure to compile. This is actually in almost any large JS library because void(0)
is less characters than undefined
and therefore you have to download slightly fewer bytes. This is one of the operations often completed by minifying tools.
“Optional” Semicolons
Look I know programmers are lazy and that’s part of the reason we’re programmers. I get it. Apparently JavaScript gets it too, since we don’t even have to bother typing those pesky semicolons like those ‘oppressive’ languages with all their boring ‘rules’ that limit my ‘creativity’ do. JavaScript lets you be a free spirit unbound from the tyranny of the semicolon.
However.
function foo() { return { a: 'bar', b: 'baz', c: 'bam' }; }
This is perfectly fine code (according to JavaScript). It will run just fine and when you call foo it will return… nothing. This function returns nothing. It return type, if JS had such an opressive and freedom limiting concept, would be that function that accepts a single argument from earlier. JavaScript is detecting that return on its own is a valid statement, so its helping you out and adding a nice little semicolon in there to help you out. Thanks JavaScript, what I wanted was to return then not reach the code that creates an anonymous object. Perfect. If you are used to programming with a style guide that enforces braces on their own lines (like I was) things like this will bite you.
I guess this is the price of freedom?