n-riesco / ijavascript

IJavascript is a javascript kernel for the Jupyter notebook
Other
2.18k stars 187 forks source link

Increment and decrement operators don't have the expected output #159

Closed krinsman closed 6 years ago

krinsman commented 6 years ago

I'm not sure if this is just a question or an actual issue, since I'm not sure what the intended behavior is. Anyway I just found this a little odd; it's obviously not important though since (I'm guessing) in Javascript ++ and -- aren't commonly used operators. Anyway,

if one does

var items = 10;
items += 1;

one gets Out[1]: 11.

But if one does instead:

var items = 10
items++;

one gets Out[1]: 10.

Entirely analogously, if one does

var boxes = 5;
boxes -= 1;

one gets Out[1]: 4.

But if one does instead:

var boxes = 5;
boxes--;

one gets Out[1]: 5.

This might make sense. If it does make sense, then I don't know why it makes sense, since I had naively expected them to give the same output.

Just to clarify, if one does

var boxes = 5;
boxes--;
boxes;

one gets Out[1]: 4, and likewise if one does

var items = 10
items++;
items;

one gets Out[1]: 11. I.e. the interpreter or whatever else is working correctly.

In yet other words, this seems to be solely an issue of display/presentation. I'm not sure if ijavascript itself is responsible for the "P" part in REPL, so even if this is an unintended quirk, it's not clear to me if this is where this should be mentioned.

n-riesco commented 6 years ago

The behaviour of items++ is defined by the JS standard. It'll make more sense to you, when you compare it to the behaviour of ++items. See MDN's documentation.

krinsman commented 6 years ago

@n-riesco Thanks for the clarification! It is appreciated, especially by a Javascript newbie like me.