They don't.
Primitive values have neither properties nor methods.
But when you do
var s = " a ";
var b = s.trim();
the value of s
is promoted to a String
object (not a primitive string) just for this operation.
It's the same for numbers and booleans.
You can check you don't really attach properties to primitive values with this code :
var s = "a";
s.prop = 2;
console.log(s.prop); // logs undefined
It logs undefined
because the property was attached to a temporary object, an instance of String
, not to s
. If you wanted to keep the property, you'd have done
var s = new String("a");
s.prop = 2;
console.log(s.prop); // logs 2