Sam Holguin
Sam Holguin

Reputation: 563

Understanding javascript void

I'm using some code to learn javascript OOP and it contains the following snippet that I'm trying to understand:

void window.setTimeout(function() {
    $(".item").css("opacity", 1)
}, 400);

I've never used the void operator, and from the documentation, can't understand why it's used in this instance?

Upvotes: 3

Views: 453

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1075309

As you will have read, the void operator evaluates its operand and then results in the value undefined. When you call setTimeout, it returns a number (the timer handle). So void setTimeout(...) results in undefined instead of a number.

If the code is really as you've shown it, there's no purpose whatsoever to the void operator there, because the return value from setTimeout isn't being used for anything.


[I've removed the bit I wrote about CoffeeScript, as I couldn't create an example; the CoffeeScript compiler complained that void is a reserved word (which it is, but that's why I was using it). I don't do CoffeeScript, so figured best to just remove that.]

Upvotes: 4

Related Questions