8.12.8 [[DefaultValue]]

2010-05-04

primitive [[DefaultValue]](hint:String=(this instanceof Date?"String":"Number"))

Return a default value for this object O. Note that the hint is just a hint, [[DefaultValue]] may return a value of any primitive type, regardless of the hint. Also note that hint can be any of these: Number, String. When no argument was passed on, hint is String. Unless this object is a Date object, then hint will be Number.
Nothing is specified when hint is not Number nor String. Host objects may specify anything they want but must ensure [[DefaultValue]] only returns primitive values.

Code: (Meta Ecma)
function [[DefaultValue]](hint){
if (hint === 'String') {
var toString = this.[[Get]]("toString");
if (IsCallable(toString)) {
var str = toString.[[Call]](O);
if (typeof str != 'object') return str;
}
var valueOf = this.[[Get]]("valueOf");
if (IsCallable(valueOf)) {
var val = valueOf.[[Call]](O);
if (typeof val !== 'object') return val;
}
throw TypeError;
}
if (hint === 'Number') {
var valueOf = this.[[Get]]("valueOf");
if (IsCallable(valueOf)) {
var val = valueOf.[[Call]](O);
if (typeof val !== 'object') return val;
}
var toString = this.[[Get]]("toString");
if (IsCallable(toString)) {
var str = toString.[[Call]](O);
if (typeof str != 'object') return str;
}
throw TypeError;
}
}

Note that the specification never supplies any other hint than Number or String.

Tricks:

Code: (Meta Ecma)
var milliEpoch = +new Date;"

Will return in a Unix timestamp in milliseconds. This is because the unary + operator (11.4.6) tries to force the Date object to a number. It will call GetValue (8.7.1) on the new object which simply returns the object. Then it applies ToNumber (9.3) on it, which converts the object to a primitive using ToPrimitive (9.1) with hint Number. ToPrimitive on an object is actually just a call to [[DefaultValue]] with an optional hint. So [[DefaultValue]] will call valueOf (15.9.5.8) and the valueOf method of Date returns the same as getTime(); its current time value.