5

This question is so basic, yet I have no idea of the answer.

Why screen object when stringified returns empty?

Does this mean JSON.stringify() needs read/write access to the input?

let a = {foo: 'one', bar: 2};


console.log(JSON.stringify(a));
console.log(JSON.stringify(screen));

4
  • 1
    @Variable developer.mozilla.org/en-US/docs/Web/API/Window/screen . If it was never declared you'd get an error about it being undefined. Commented Jul 27, 2018 at 11:10
  • 7
    Possible duplicate of Stringify DOMWindow object Commented Jul 27, 2018 at 11:11
  • @ADyson Thanks, didn't knew about this, learned something new :D. 👍 Commented Jul 27, 2018 at 11:17
  • @user202729 I think the answer here is better than the one in the duplicate. I'll change the tittle so that it's more general. Commented Jul 27, 2018 at 11:29

1 Answer 1

11

FROM MDN Network

For all the other Object instances (including Map, Set, WeakMap and WeakSet), only their enumerable properties will be serialized.

Read https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable

    console.log((window.screen));
    console.log(JSON.stringify(window.screen));
    console.log(window.propertyIsEnumerable(screen));

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

1 Comment

Actually, most of the (popular) answers on this site are just a docs-quote. That is a proper answer (as long as you quote important parts and not just a link)

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.