10

I understand that the following code wraps a number into an object:

var x = Object(5);

I therefore expect and understand the following:

alert(x == 5); //true
alert(x === 5); //false

However, I also understand that an object is a list of key/value pairs. So I would have expected the following to be different:

alert(JSON.stringify(5)); //5
alert(JSON.stringify(x)); //5

What does the structure of x look like? And why does it not appear to be in key/value pair format?

5
  • What's at issue here is the behavior of JSON.stringify(), is that right? Commented Jan 3, 2016 at 20:41
  • I was not sure. I guess I took the definition that all objects are key/value pairs too literally and as a result, expected something in a key/value pair format. Commented Jan 3, 2016 at 21:06
  • Well that's kind-of what I mean; the output from JSON.stringify() in your question is the result of how that particular API works. It explicitly understands that Number instances should be treated as numeric values in the resulting JSON. Commented Jan 3, 2016 at 21:07
  • Similarly, String instances are treated as if they were string primitives, and Boolean instances as boolean primitives. Commented Jan 3, 2016 at 21:07
  • But .... the object still has key/value pairs, the Number object has a primitiveValue property, where the value is 5, it's just like Pointy explains, some API's understand the difference between a "regular" object, and an object just holding a primitive value. Commented Jan 3, 2016 at 21:10

3 Answers 3

11

The Object constructor creates an object wrapper for the given value, of a Type that corresponds to the value.

So you get a Number object with the primitive value 5 when passing a number to Object

var x = Object(5);

it's exactly the same as doing

var x = new Number(5);

when calling valueOf() on both, you get the primitive value 5 back again, which is why stringifying it gives the same as stringifying the number 5, the object is converted to it's primitive value before stringifying

The specification for JSON.stringify says

Boolean, Number, and String objects are converted to the corresponding primitive values during stringification, in accord with the traditional conversion semantics.

Sign up to request clarification or add additional context in comments.

2 Comments

So the following return false because it's a different instance? var x = Object(5); var y = new Number(5); alert(x === y);
@Sandy - yes, when comparing directly and strict, it's still objects, and two objects are never the same
0
var x = Object(5);

This boxes 5 as an object, so Stringify just unboxes the value.

If you assign a key/value to an object, Stringify will display as such :

var x = {};
x.foo = "bar";

This is javascript Duck Typing - basically if it looks like a duck, and sounds like a duck, it must be a duck however replace duck with a data type, such as int or collection... https://en.m.wikipedia.org/wiki/Duck_typing

Comments

0

In a javascript console, I entered the following:

> var x = Object(5);
> x
[Number: 5]
> JSON.stringify(5)
'5'
> JSON.stringify(x)
'5'

When you use Object(5), you are creating an object with key/value pairs. However, JSON.stringify() is turning that object into it's string representation -- "5". Calling JSON.stringify() on a literal value like the primitive number 5 also returns it's string representation -- "5". You're converting both the object and the primitive to strings, that's why they are equal.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.