- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
Several times in various posts on this blog, I've mentioned some differences between JavaScript primitive values and their corresponding objects. The other day one of my readers asked me to elaborate a bit on this topic, as he found the whole thing very confusing. "Why are there two ways to represent a string or a number?" he asked, a bit plaintively. "How am I supposed to know which one is actually being used in any given situation, and why does it even matter?"
Well, it's like this...
The first thing to understand is that when JavaScript evaluates an expression, that evaluation always uses primitive values. So, for instance, if I were to run this code:
var x = new Number(54);
var y = new Number(45);
var z = x + y;
When JavaScript evaluates the expression x + y, it first converts the Number instances to primitive number values, by calling the valueOf() method on each of them. The same thing is true for expressions whose operands include instances of String) or Boolean) as well. Any type coersion needed is done after the operands have been converted to primitive values. So you needn't worry about whether your expressions include primitive values or objects; they all end up as primitive values when they are evaluated.
Their are certain situations in which JavaScript will automatically convert a primitive value to a corresponding object. Consider this code (which works just fine):
var x = 532;
gs.log(x.toFixed(3));
After the first line is executed, the variable x contains a primitive number value. In the second line, that value is converted to a Number instance, and then the toFixed() method on that instance is called. The instance is then discarded, and the variable x remains a primitive number value.
Because these conversions happen automatically and (for the most part) seamlessly, it's not very often that you really need to explicitly use one of the three value object types (Number, Boolean, or String). I can only think of two occasions when I've felt the need to use these value object types explicitly.
First (and by far the more common need) is when I really needed to force JavaScript to coerce a value to a particular type. For example, consider this code:
var x = '1234';
var y = new Number(x).valueOf();
gs.log('x: ' + typeof x);
gs.log('y: ' + typeof y);
If you run this code, you'll see that x is a primitive string and y is a primitive number, just what I wanted. But this code does the same thing, more concisely:
var x = '1234';
var y = x - 0;
gs.log('x: ' + typeof x);
gs.log('y: ' + typeof y);
This is a good demonstration of how JavaScript evaluates expressions. It sees the minus sign and figures out that this must be a numeric expression (since minus signs can't appear in string or boolean expressions). So it coerces x to a primitive number value and subtracts zero from it (which doesn't change the value, of course), and then assigns the primitive number result to y. Yes, it's a bit of a trick — but it sure is much less of a mouthful than the previous method.
The second reason I've explicitly used the object types is when I wanted (for whatever reason) to add properties to the value. If you're more used to other programming languages than you are to JavaScript, you may find this a bit of a mind-bender. But...in JavaScript, all the object types are child classes of the base class Object — and the Object class (and therefore, all JavaScript classes) allows you to add new properties anytime you'd like to. Check out this code:
var x = 12;
gs.log(typeof x);
x.a = 'OMG';
gs.log(x.a);
x = new Number(12);
gs.log(typeof x);
x.a = 'OMG';
gs.log(x.a);
The first four lines fail dismally, because you can't add properties to a primitive value. But the second four lines work great, because I've explicitly created my numeric value as an instance of Number.
Any other questions about primitive values versus the corresponding object types?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.