2

Whats the difference between

Object.assign({},obj1,obj2);

and

Object.assign(true,obj1,obj2);

I know what obj.assign does but whats the use of the latter one ? it returns a boolean type with obj1 and obj2 to merged into it.

PS: It was an interview question so would like to know whats the use case of this.

6
  • 1
    There is no sane use of the second one. I would hope never to see that in a codebase, ever. Commented Oct 6, 2018 at 7:09
  • 1
    No it works perfectly fine, no errors. Commented Oct 6, 2018 at 7:09
  • 1
    @CertainPerformance I agree, i was wondering the same but since they asked in the interview i thought it might be important. Commented Oct 6, 2018 at 7:10
  • 2
    It's not important, it's just silly trivia I think. Yes, Boolean objects can have properties assigned to them, but they really shouldn't. Commented Oct 6, 2018 at 7:11
  • 1
    Object.assign converts the first argument to an object if it isn't already (ecma-international.org/ecma-262/9.0/#sec-object.assign). Boolean objects don't have any use. Commented Oct 6, 2018 at 7:11

2 Answers 2

2

As outlined above, primitives get "boxed", so the following:

 Object.assign(true, obj1, obj2)

is merely the same as:

 const bool = Object.assign(new Boolean(true), obj1, obj2)

now boolean objects are just regular objects, that return a boolean when valueOf() is called on them. That means you get some funny behaviour:

 bool === true // false
 bool == true // true
 +bool === 1 // true

You cannot even use that Boolean object inside conditions as all objects are truthy:

  if(Object.assign(false,{a: 1 }))
   alert("works");

So actually there is no difference between a regular object and a Boolean object, except that the latter adds some confusion and serves no purpose whatsoever.

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

1 Comment

just side note: .valueOf() returns primitive value back. So it's possible to unbox it for using it in if().
1

Besides boolean is supposed to be primitive type it has object-type wrapper that is... surprise Boolean.

In case of

Object.assign(true,{a: 1}, {b: 2})

there is such boxing(term from Java/C# worlds I really like) happens. And then Object.assign works over this object-type wrapper as it should. That's why result looks in console like

Boolean {true, a: 1, b: 2}

Boolean's object like every other objects can be extended with custom keys/values. Output looks confusing but if you expand this in console you will see there is no actually "value of true without any key" - so it's just tricky output.

BTW exactly the same happens to strings when you tries to call some method on primitive value like

'12345'.split(/./)

It is converted into object with String() constructor and then call String's method.

And yes, I believe it should be never used in real life code.

Here is spec says:

  1. Let to be ToObject(target).

So explicit converting primitive-to-object is very first step.

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.