Simple JS operand

I got curious about something, but, let's present the following case:
let a = 2;
let b = 5;

a *= 2 + b;
console.log(a); // 14
let a = 2;
let b = 5;

a *= 2 + b;
console.log(a); // 14
Why is it a = a * ( 2 + b ), instead of a = a * 2 + b. Is there any logic behind it? Or do I just accept the fact that a short hand for *= would assume anything after would be in a parenthesis?
33 Replies
Joao
Joaoā€¢16mo ago
I don't think there's any logic behind it, it's just a shortcut to multiply (and re-assign) the variable a by the value on the right, so naturally it must come in parenthesis. Or in other words, it must be resolved before the operation can be performed.
13eck
13eckā€¢16mo ago
Why? Because that's how it's designed to work:
The multiplication assignment (*=) operator multiplies a variable by the value of the right operand and assigns the result to the variable.
ā€“https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Multiplication_assignment The right-side of the = is evaluated then multiplied by the left-side. If you want it to work differently you can't use the multiplication assignment operator.
Myndi
Myndiā€¢16mo ago
I see, so it's just life. Makes sense.
13eck
13eckā€¢16mo ago
If you expect JS to make sense, I have some real bad news, my friend
Myndi
Myndiā€¢16mo ago
I'm not expecting anything to be frank. I just wanted to understand if there was any logic behind it. If there's not, that's just the new logic to me.
13eck
13eckā€¢16mo ago
There is logic, yes. In this case not the logic you expect [the more you know].gif
Myndi
Myndiā€¢16mo ago
This is a bit more tricky to me, but why exactly is this a string called "NaN"?
let grapes = "grapes";
let bananas = "bananas";
let pears = "";

let fruits = grapes + 7 + bananas - 7 + pears
console.log(fruits); // "NaN" -> typeof "string"
let grapes = "grapes";
let bananas = "bananas";
let pears = "";

let fruits = grapes + 7 + bananas - 7 + pears
console.log(fruits); // "NaN" -> typeof "string"
13eck
13eckā€¢16mo ago
Because grapes, bananas and pears are not numbers. If you try to add a not-number to a number you get NaN (Not a Number)
Myndi
Myndiā€¢16mo ago
But it's a "NaN" string? It's not just a NaN, it's a string.
13eck
13eckā€¢16mo ago
That's because "NaN" is, indeed, a string That being said, typeof Number.NaN is Number :p
Myndi
Myndiā€¢16mo ago
So, let fruits = grapes + 7 + bananas - 7 = NaN But let fruits = grapes + 7 + bananas - 7 + pears = "NaN"? Why is that I guess that's why I'm confused.
13eck
13eckā€¢16mo ago
Here's the thing, "NaN" is a string, but NaN (without the quotes) is of type Number. It's Number.NaN However, the JS parser doesn't give you Number.NaN it gives you "NaN" the string. For some reason
Myndi
Myndiā€¢16mo ago
Shouldn't it just be NaN?
13eck
13eckā€¢16mo ago
It should, but for some reason it's not
Myndi
Myndiā€¢16mo ago
Myndi
Myndiā€¢16mo ago
That's why it had me dogekek
13eck
13eckā€¢16mo ago
Interesting Obviously it has something to do with the empty string represented by pears but I have no idea what
Myndi
Myndiā€¢16mo ago
šŸ¤” I was doing some exercises, and started trying things, and came across this on my own. And I was confused if I learnt it incorrectly, or not. At least my understanding was: "string" (grapes) + "number" (7) => "string" (grapes7) "string" (grapes7) + "string" (bananas) => "string" (grapes7bananas) "string" (grapes7bananas) - "number" (7) => NaN NaN + "string" ( empty ) => "string"? (NaN)? It's like adding the NaN into the empty string and treating it as a "string".
13eck
13eckā€¢16mo ago
INTERESTING If the last operand is ā€“ then it's Number.NaN!
Myndi
Myndiā€¢16mo ago
Well, depends, if the previous is a string with a number inside of it, and no letters, it will treat it as a number. But if it has any letter, it will be treated as a string, hence NaN. So "7" - 7 = 0, but "7px" - 7 = NaN.
13eck
13eckā€¢16mo ago
Well, yes. That's how JS works: if it sees a math operator then it tries to coerce things to numbers
Myndi
Myndiā€¢16mo ago
But I still don't understand where the logic comes from to make NaN a Worryohno
13eck
13eckā€¢16mo ago
The + operator can be used for two things: ā€¢ Addition ā€¢ String concatenation So if there's a string that can't be coerced into a number it treats it as multiple strings being concatenated. However, the subtraction (and other mathematical) operator (ā€“) is only used for maths. It will coerce everything to a number, and if it can't be for some reason ("apple") then it becomes Number.NaN. And if you try to do any maths on Number.NaN you will get Number.NaN. So for your above examples, any that don't include the subtraction operand will treat it like a string and make it a string at the end (even if that "string" is Number.NaN) And because JavaScript follows mathematical order of operations, if the subtraction is not the last operator, then it'll be coerced back into a string if there's another + [string] afterwards
let grapes = "grapes";
let bananas = "bananas";
let pears = "";
/*
"grapes" + 7 = "grapes7"
"grapes7" + "bananas" = "grapes7bananas"
"grapes7bananas" - 7 = NaN
NaN + "" = "NaN"
*/
let fruits = grapes + 7 + bananas - 7 + pears
let grapes = "grapes";
let bananas = "bananas";
let pears = "";
/*
"grapes" + 7 = "grapes7"
"grapes7" + "bananas" = "grapes7bananas"
"grapes7bananas" - 7 = NaN
NaN + "" = "NaN"
*/
let fruits = grapes + 7 + bananas - 7 + pears
Another thing that dilutes the "what the eff is the + anyway?" is that it also is the Unary Plus operator! If anything is proceeded by + then it makes it a number! +[something] is a shortcut for Number([something]). The plus sign is indeed multitalented and multi-annoying!
Myndi
Myndiā€¢16mo ago
Thank you for the explanation. I was going through javascript.info course in the operators.
13eck
13eckā€¢16mo ago
I hope it makes some sort of sense >_<
Myndi
Myndiā€¢16mo ago
I was simply confused for the behavior. The JS.info course explains it pretty well.
13eck
13eckā€¢16mo ago
Nice!
Myndi
Myndiā€¢16mo ago
But as usual, my intrusive thoughts wanted to try different examples. And that's how I got pepeLaughing
13eck
13eckā€¢16mo ago
JS.info is great Trying and succeeding or failing is how we grow Today you learned something you wouldn't have if you didn't try to break things.
Myndi
Myndiā€¢16mo ago
Basically. My intrusive brain always wants to break what I'm given, lol.
13eck
13eckā€¢16mo ago
I try to break shit all the time. Some times it breaks and sometimes I break lol
Myndi
Myndiā€¢16mo ago
Thank you for the help :)
13eck
13eckā€¢16mo ago
Of course!