0

why java script convert string to number when I'am put only + before string

var x = "44";
var y =(+x); 
var z = 34 + x;  
console.log(typeof y); //number
console.log(typeof z); //string
Moneim
  • 33
  • 5
  • That's what the unary `+` operator is supposed to do. – Pointy Apr 02 '20 at 22:05
  • 1
    Does this answer your question? [What's the significant use of unary plus and minus operators?](https://stackoverflow.com/questions/5450076/whats-the-significant-use-of-unary-plus-and-minus-operators) – SuperStormer Apr 02 '20 at 22:08

1 Answers1

1

From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators:

+ is the Unary operator. It attempts to convert the operand to a number, if it is not already.

Arnav Bansal
  • 11
  • 1
  • 2