When you call a method on a variable of a primitive type, so-called automatic boxing occurs. This process wraps a primitive value in the corresponding object, for example, 'asdf' - new String('asdf') . Since technically primitive values do not have methods and properties, they are placed in object prototypes. With automatic boxing, you can call methods for primitive values. And inside the method, this always an object that has this method.
If you want to access a primitive value in a method, you can either pass it as an argument, or you want to get a primitive value from this . For instance:
var str = new String('asdf') // String {0: "a", 1: "s", 2: "d", 3: "f", length: 4, formatUnicorn: function, truncate: function, splitOnLast: function, [[PrimitiveValue]]: "asdf"} String(str) // 'asdf' var num = new Number(123) // Number {[[PrimitiveValue]]: 123} Number(num) // 123 var bool = new Boolean(true) // Boolean {[[PrimitiveValue]]: true} Boolean(bool) // true
source share