如果我输入一个数字作为输入,为什么这会导致连接而不是将两个数字加在一起?
let theNum = n => n + 1;
let usrNum = prompt("Enter a number: ")
alert(theNum(usrNum))
最佳答案
因为在JS中,字符串+数字=字符串。在 n 之前尝试 '+':
let theNum = n => +n + 1; // or Number(n)
let usrNum = prompt("Enter a number: ")
alert(theNum(usrNum))
关于javascript - 为什么这会导致串联而不是将两个数字相加?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59120716/