Primitive Coercion In JavaScript
Primitives are basic values which have no properties and cannot be inherited from. There are only 5 primitives in most JavaScript implementations; null, undefined, number, string and boolean. So anything else you encounter is an object with all the associated implications like property assignment. Including functions. Also, unlike objects, they cannot be modified. Only combined and assigned to form new primitives. Although they do not have properties, they are automatically coerced into objects in certian situations:
console.log((4).toString());
The grouping operator transforms 4 into an Object. Since all objects inherit the toString method from Object that now can be called. But the same does not work for undefined or null:
console.log((undefined).toString());// error console.log((null).toString());// error
Finally, booleans and strings are automatically coerced into Boolean and String objects respectively whenever properties are called on them:
console.log(true.toString());// true console.log(false.toString());// false console.log('test'.length);// 4
There may be other more obscure ways to coerce, but this should help diagnose some potientially wierd looking syntax.
Github Location https://github.com/Jacob-Friesen/obscurejs/blob/master/2015/primitiveCoercion.js