Unary operators with function expressions


#1

The origin of this post is a conversation in another thread, which I think is interesting, but also a bit of a hijack from the question of the OP there.

I ran tests on unary operators with function expressions. Typescript 2.7.1, though that shouldn’t matter.

let test = !function(x) { return x } (true);
console.log(test); // false

let test = +function(x) { return x } (5);
console.log(test); // 5

let test = +function(x) { return x } (true);
console.log(test); // 1

let test = -function(x) { return x } (5);
console.log(test); // -5

let test = -function(x) { return x } (true);
console.log(test); // -1

let test = ~function(x) { return x } (5);
console.log(test); // -6

let test = ~function(x) { return x } (true);
console.log(test); // -2

TypeScript button work only one time
#2

In the spirit of “writing useful yet hard-to-read JavaScript”, that bitwise complement operator (~) in your last two tests has the side effect of converting its operator into an integer, so ~~x has the same meaning as Math.floor(x).


#3

My usual response to javascript holds true here. :exploding_head: (that’s my brain exploding, fyi)