1

Why is the output of the following code is 1

function test(){} + 1; // output: 1
Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
Mohamad Shiralizadeh
  • 8,329
  • 6
  • 58
  • 93

2 Answers2

3

Because of automatic semicolon insertion, that code is actually processed as:

function test(){}; + 1;

That's the unary plus operator, not the addition operator.

Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
  • 1
    Also worth noting that it's ([almost](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval)) impossible to have this code in a real application and get `1` as a result. If you're *receiving* a value out of this, then it must be an expression. If it's an expression, then that's not a function declaration and thus it would be processed effectively as as `( (function test(){}).toString() + 1)` which produces the string `"function test(){}1"`. The only way to get a `1` is in a REPL as you'd get the ASI assisted interpretation and the value of the last statement. – VLAZ Jul 02 '20 at 12:17
  • It seems pretty straightforward to deduce that the question refers to REPL output rather than log output given there's no `console.log()` present. I added REPL to the question tags, the rest of your comment about the statement being in expression position seems moot since there's a semicolon present making it impossible for the question to be referring to expressions without it having purposefully omitted other code. – Patrick Roberts Jul 02 '20 at 12:22
  • "*It seems pretty straightforward to deduce that the question refers to REPL output*" only if you already know *why* that would produce a `1` and that it's only in REPL. So it's easy to know if you already know it - I agree with that. However, the question could easily be seen as referencing `x = function test(){} + 1` but just cut out the assignment for brevity. Or maybe it cut out the `console.log()` for brevity. [There is a comment of that interpretation](https://stackoverflow.com/questions/62695923/why-is-the-output-of-function-test-1-is-1/62696042?noredirect=1#comment110872251_62695923) – VLAZ Jul 02 '20 at 12:27
-2

function test() or {} here is not an Object, its en empty statement and JS Cannot convert object to primitive value and find the safe route and convert this statement value to false.

{} + 1 = 1 because (false + 1) = always 1.

Krishan Kumar
  • 394
  • 4
  • 13
  • `function test(){}` is most definitely not an empty block. Even `function test(){ let foo = "hello"; let bar = foo + "world"; console.log(bar); return bar; } + 1` in a REPL environment will exhibit the same behaviour. Furthermore even if you *do* have an empty block it will not be converted to `false` - it will simply be ignored. There is literally no value produced there, not even `undefined`. The actual expression evaluated is `+ 1` - a unary plus and the number one. So, it's not a binary operation as there aren't two operands, it's a unary one. – VLAZ Jul 02 '20 at 15:10
  • Great reply @VLAZ . Thank you for clearing this. Also if you don't mind I want help from you. I am on the way to Become Expert in JS Like You. What is the source/site/url from where I can clear my basic fundamentals and doubts. – Krishan Kumar Jul 02 '20 at 18:07
  • 1
    Depends on what you need. [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference) is always useful - if you want to find how something works, I definitely recommend searching there. For example, if you want to know about `.map` in arrays, just search for `mdn array map`. [AST Explorer](https://astexplorer.net/) is also very useful in breaking down what different expressions would be parsed and processed. [JavaScript.info](https://javascript.info/) is usually insightful, as is [2ality](https://2ality.com/). Stay away from W3Schools - usually has incomplete or wrong information. – VLAZ Jul 02 '20 at 18:21
  • Great and Thank You @VLAZ – Krishan Kumar Jul 02 '20 at 18:37