0
var b = true;
b.foo = 'whatever'; // Auto-boxing occurs?
b.foo; // undefined - why?

Can I retrieve the value of property foo now?

4

1 Answer 1

0

var b is initially set to Boolean value. to assign dot notation to a variable, it has to be a javascript Object. if b is set as var b = {}, b.foo = 'whatever'; should work. For better practice always check type of variable before switching its datatype:

var b = true;
if(typeof b === 'object'){
  b.foo = 'whatever';
}
Sign up to request clarification or add additional context in comments.

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.