0

as you show below, when javascript doing an arithmetic operation all value concatenation with the string it shows a string value but I have some confusion...

var x = 10;
var y = 20;
var sum = x + y;
console.log("sum is :" + sum); //this is number
But confusion is

var x = 10;
var y = 20;
console.log("sum is : " + 10 + 20 ); //why this is string

var x = 10;
var y = "The value is " + x; // why this is string 

var x = 10;
var y = 20;
var sum = x + y;
var z = 'sum is' + sum; //why this string
console.log("sum is : " + sum) // why this is not string coz it is also concatenation with string. 
3

3 Answers 3

2

JavaScript will concatenate and coerce in a certain order of operations. You can add parentheses to add numbers before coercing to a string.

console.log("sum is : " + 10 + 20); // sum is : 1020
console.log("sum is : " + (10 + 20)); // sum is : 30

1

The unary + operator can be used to convert a variable to a number:

var y = "5";      // y is a string
var x = + y;      // x is a number

If the variable cannot be converted, it will still become a number, but with the value NaN (Not a Number):

var y = "John";   // y is a string
var x = + y;      // x is a number (NaN)

When JavaScript tries to operate on a "wrong" data type, it will try to convert the value to a "right" type.

5 + null    // returns 5         because null is converted to 0
"5" + null  // returns "5null"   because null is converted to "null"
"5" + 2     // returns "52"      because 2 is converted to "2"
"5" - 2     // returns 3         because "5" is converted to 5
"5" * "2"   // returns 10        because "5" and "2" are converted to 5 and 2

So if you put numbers inside parenthesis like (10 + 20) then it will perform arithmetic operation first then it will do the concatenation outside. If either one of them would be string then it would do the concatenation inside as well.

var console.log("sum is : " + (10 + 20) ); // sum is : 30
var console.log("sum is : " + (10 + '20') ); // sum is : 1020
1

When you are adding a number with a string it counts the number as a string, like console.log("sum is : " + 10 + 20 ). But when 10 and 20 is under a variable it counts the number as a variable value. If you want to use numbers with a string use "sum is: " + parseInt(10) like this.

3
  • What's the point of parsing an int to an int value, as you are suggesting? parseInt() expects a string as argument. But then you concatenate the result to a string, so it is converted to a string again.
    – nucleaR
    Commented Jul 3, 2020 at 8:38
  • When you want a string that needs to be an int or number then you can parse it . as there addressing 10 and 20 without any variable so the interpreter assumes the number as a string. that's why I suggest using parseInt() :)
    – Mohiuddin
    Commented Jul 3, 2020 at 9:12
  • Indeed if you need a string to be an int you should parse it. But the value of 10 is already an int. The problem of OP is caused solely by the order of operation. Instead of using parseInt, you should simply use parentheses around (10+20).
    – nucleaR
    Commented Jul 6, 2020 at 9:33

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.