I'm wondering what happens behind the scenes when we either add, subtract or multiply two strings of numbers. Here is an example:
let strNum1 = "300";
let strNum2 = "22";
let multiply = function(num1, num2) {
let product = num1 * num2;
return `${product}`
};
multiply(strNum1, strNum2); //this will return ==> "6600"
Does the JS engine turns these into integers first and then performs the operations or does it know "magically" that they are numbers even though it's in a string form? The reason I'm asking is because of the long multiplication algorithm. For numbers bigger than 8 chars it becomes funky when multiplying with the operator vs using the algorithm.
This is a leetcode question btw.
*operator coerces the strings into numbers, that'll happen with-,/,%, etc.+is the exception it'll be interpreted as concatenating of two strings. You can still use+to coerce string into number by enclosing the right side string in parenthesis:"1" +(+"2")