Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "void 0"? [duplicate]

Tags:

javascript

I'm learning Javascript by reading some code, but this function really puzzles me.

    hv:
        function(i) {
            var _this = this;
            return isArr(_this) ? _this.indexOf(i) > -1 : _this[i] !== void 0;
        }

This function is added to Object.prototype.
I don't quite get the void 0 at the end of the ternary expression. Can someone explain it to me?

Thanks.

like image 466
octref Avatar asked Feb 14 '23 10:02

octref


1 Answers

The void operator is often used merely to obtain the undefined primitive value, usually using “void(0)” (which is equivalent to “void 0”). In these cases, the global variable undefined can be used instead (assuming it has not been assigned to a non-default value).

in this cases, the global variable undefined can be used instead: ie:

_this[i] !== undefined;

Jsfiddle Demo

like image 69
suhailvs Avatar answered Feb 16 '23 04:02

suhailvs