Assuming we’ve all read https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var, still might be some surprises:


console.log(bob);

No surprise, throws an Error.

var bob;
console.lob(bob);

No surprise, logs undefined (no error)

console.log(bob);
var bob;
console.lob(bob);

Surprise – *both* log undefined, no error is thrown, even on the first line before bob is “initialized”.

var SomeClass = function(){};
SomeClass.prototype.name;
var bob = new SomeClass();
console.log('name' in bob);

Surprise, Logs false.

var SomeClass = function(){};
SomeClass.prototype.name = undefined;
var bob = new SomeClass();
console.log('name' in bob);

Surprise, logs true, despite it being functionally equivalent to the previous block.


One big takeaway for me is to always initialize prototype values to something that makes sense (null for objects, arrays and instances, empty strings for strings, false for booleans, maybe 0 for numbers).