Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding javascript void

Tags:

javascript

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?

like image 896
Sam Holguin Avatar asked Nov 21 '25 00:11

Sam Holguin


1 Answers

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.]

like image 80
T.J. Crowder Avatar answered Nov 22 '25 14:11

T.J. Crowder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!