2

There is a portuguese IT company that is looking for some developers in a few areas and just out of curiosity (since I already have a job, thankfully) I went to check the job postings.

When I went to check out the JS developer posting, they provided a piece of JS code that caught my attention. I've worked for some time with JS and I find myself getting back to programming with JS from time to time but to be honest I've never seen anything even similar to the code given.

This is the code:

!(function(x){
    '6D 61 6E'.split(' ').forEach(function(a){
        x+=String.fromCharCode(parseInt(a,16));
    });
    return x;
})('');

I went and wrote this on Chrome's JS console and the output is 'false'. If I understand it correctly, the "strange" code, and according to the ASCII table reads 'm a n', and parseInt is supposed to return an integer based on a hexadecimal radix. It then gets converted once again into a string, this time based on the chars decimal value. To finish it all, we evaluate the return 'x' by "negating it" (not the word I was looking for but can't remember a better one at the time... evaluate maybe?).

Then, why is the output false? If we don't evaluate the return the result is the expected one 'man', but I don't see why we get false on this particular instance.

Anyone care to elaborate?

2
  • 7
    What does that have to do with addEventListener(). Commented May 22, 2013 at 10:09
  • @nnnnnn My mistake, I had SO open to ask another question which I didn't come to complete and forgot to change the title.. ;) Commented May 22, 2013 at 10:23

1 Answer 1

3

As you seem to have worked out,

return x;

...will return the string "man". But your question seems to boil down to why !"man" gives false?

From MDN, logical not !:

Returns false if its single operand can be converted to true; otherwise, returns true.

The empty string "" is falsey, so !"" is true, but any other string is truthy, so !"any other string" is false.

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

1 Comment

@nnnnnn I came to post this question as I've tried using other strings instead of the empty string and I still got false as an output, but I'm quite sure that your explanation is the right one

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.