Skip to content

Instantly share code, notes, and snippets.

@jacopotarantino
Created March 5, 2018 18:47
Show Gist options
  • Save jacopotarantino/cbe310d0042527cddc497ce9c709e7a3 to your computer and use it in GitHub Desktop.
Save jacopotarantino/cbe310d0042527cddc497ce9c709e7a3 to your computer and use it in GitHub Desktop.
Impossible JavaScript Question
/* what do each of these statements return?
(assume that they are evaluated individually but in order.)
*/
const foo = 'asdf'; //=> ?
foo.bar = 12345; //=> ?
foo; //=> ?
foo.bar; //=> ?
Object.getOwnPropertyNames(foo); //=> ?
@stellis
Copy link

stellis commented Mar 5, 2018

What is the point? Are you trying to show that people don't know what assignment statements are going to return? I think that's true in most languages because it's so unusual to use the return value of an assignment for anything.

I think there's value in showing that a language does weird things when used in common ways, but if you do something unusual (like adding new properties to a string), of course most people won't know what will happen.

(I'm not a fan of JS at all, just not sure this is the way to convince people that it's a bad tool.)

@jacopotarantino
Copy link
Author

Hehe. This is actually derivative of some code I found that took me and another programmer some time to split up and figure out what it was doing and if that's even possible. What really got me is that the assigning to a property of a string seems like it should throw an error but it doesn't (in most JS environments). So we turned it into a reduced test case and started asking people to solve it. It's not really for proving that people don't know JS or making a huge statement about the language. It's an illustration of what happens when you write really weird/bad code. As an extra bit of fun, it's a nifty way to pry at how people thing the language does and should work.

@EionRobb
Copy link

EionRobb commented Mar 6, 2018

I didn't realise the second one behaved differently without strict mode. Are we assuming non-strict?

@mgd020
Copy link

mgd020 commented Mar 6, 2018

What's the use of knowing this? You wouldn't do it in production so who cares?

@jacopotarantino
Copy link
Author

@EionRobb - I hadn't even thought about strict mode yet. Good catch!
@mgd020 - Shits and giggles lol. This actually came up because of production code so somebody did it in production.
@gkaykck - Haha. Well done ;). I absolutely don't expect people to know this stuff by heart. That's why it's such a weird challenge. This isn't stuff that you should know because you should never be doing this nonsense to begin with.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment