Why it is treated as string if we use first parameter as string not in case of number

If we do this console.log(“50”+10+20+30+40); the output will be 5010203040.
But If we do this console.log(10+20+30+40+“50”); the output will be 1050.
In first case the first parameter is string so it will treat the whole thing as a string.
But in second case the first parameter is number then it will concatenate with string.

When the compiler starts executing, it will check the data type. If it is “string”, then it will perform string concatenation or else it will try to perform “addition”, if the data type is “number” till the end of the inputs.