This question already has an answer here:
What's the significant use of unary plus and minus operators?
                            
                                (1个答案)
                            
                    
                上个月关闭。
        

    

为什么当我在字符串前仅加+时,java脚本将字符串转换为数字

var x = "44";
var y =(+x);
var z = 34 + x;
console.log(typeof y); //number
console.log(typeof z); //string

最佳答案

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

+是一元运算符。如果尚未将操作数转换为数字,它将尝试将其转换为数字。

关于javascript - Javascript将字符串转换为数字,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61001888/

10-09 18:36