3 ms·
It depends. x = {} + 12; x has the value "[object Object]12". So, it' not always 12 :P
by dorgo 5y ago
It depends.
x = {} + 12;
x has the value "[object Object]12". So, it' not always 12 :P
- readonthegoapp 5y agocan confirm: https://replit.com/@atlwellwell/EsotericJSInterviewQuestion#index.js https://replit.com/@atlwellwell/EsotericJSInterviewQuestion#... at this point, i'd love to know when the answer is just '12'. i remember going at it with some dude back in the day who was 100% certain, and got visibly upset, when i stood my ground that java only used pass by value. i allowed that even object references were being passed, but that they were passed by value. i didn't get the job.
- dorgo 5y ago>at this point, i'd love to know when the answer is just '12'. Ctrl+Shift+k in a browser (firefox) opens a console. Just write {}+12 there and hit enter. My guess is that {} is (in some circumstances) evaluated as a block of code (instead of an empty object). Since the block is empty it evaluates to undefined. The +12 is considered another block of code ( the + operation takes here only one parameter - so it is not a sum ). Consecutive blocks of code are evaluated to the value of the last block of code, which is +12. {}/12 for example doesn't work at all - which is a hint that the + is not a summation operation. Edit: I'm wondering why we use 12 and not 42 Edit2: Technically you are correct about java being pass by value. But at this point I'm questioning the sanity of the distinction. It feels more and more like a philosophical discussion.