简体   繁体   English

为什么即使输入的数字是 Javascript 中的字符串,年龄计算器也能正常工作?

[英]Why does age calculator work even when the input number is a string in Javascript?

In prompt the user's response is a string.prompt中,用户的响应是一个字符串。 Even if the response is a number, it comes back as a string.即使响应是数字,它也会以字符串形式返回。 So I did some addition in the console:所以我在控制台做了一些补充:

var numberOfCats = prompt("How many cats?");
var tooManyCats = numberOfCats + 1;

If I type number 3 in the prompt box and then check tooManyCats , the result is: 31 .如果我在prompt框中键入数字3 ,然后选中tooManyCats ,结果为: 31 Because Javascript can't do math using a string and a number.因为 Javascript 无法使用字符串和数字进行数学运算。 So it adds the number 1 right beside it.所以它在它旁边添加了数字1

But for this example, for some reason it doesn't mess up the code.但是对于这个例子,出于某种原因它并没有弄乱代码。 Here I made a age calculator in the console.这里我在控制台做了一个年龄计算器。

var age = prompt("What is your age?")
var days = age * 365.25; //.25 is for the leap years
alert(age + " years is roughly " + days + " days");

If I type 20 in the prompt box, it alerts me: 20 years is roughly 7305 days如果我在prompt框中输入20 ,它会提醒我: 20 years is roughly 7305 days

I can't understand why the second one works without any issues.我不明白为什么第二个没有任何问题。 Is it because of Javascript seeing the string and number in it's own unique way or is there some reason behind it?是因为 Javascript 以自己独特的方式看到字符串和数字,还是背后有某种原因?

It works because unlike + , * converts its operands to numbers if necessary before doing the multiplication, so * expressions always result in a number value (even if that value is NaN ) (or a BigInt if both operands are BigInts), whereas + may result in a number (or BigInt) or a string, depending on its operands.它之所以有效,是因为与+不同, *在进行乘法之前必要时将其操作数转换为数字,因此*表达式总是产生一个数字值(即使该值是NaN )(如果两个操作数都是 BigInts,则为 BigInt),而+可能结果是一个数字(或 BigInt)或一个字符串,这取决于它的操作数。 (In fact, + is the only one of the classic math operators that doesn't always result in a number or a BigInt; - , / , % , and * always do). (事实上, +唯一一个并不总是产生数字或 BigInt 的经典数学运算符; -/%*总是如此)。 So days is a number, since it's the result of * (on something other than BigInts).所以days是一个数字,因为它是* 的结果(在 BigInts 之外的其他东西上)。


About BigInts: The only time + , - , / , * , and % operator expressions result in BigInts is if both operands are BigInts, there is no implicit conversion.关于 BigInts:唯一一次+-/*%运算符表达式导致 BigInts 是如果两个操作数都是 BigInts,则没有隐式转换。 If only one of the operands is a BigInt, the operation throws a TypeError.如果只有一个操作数是 BigInt,则该操作会引发 TypeError。

Javascript decides automatically if the input you entered is a string or a number. Javascript 会自动判断您输入的是字符串还是数字。 Sometimes its not correct and its up to you to change it.有时它不正确,由您来更改它。 You can use Number() method to transform string into number.您可以使用 Number() 方法将字符串转换为数字。 Or as mentioned by TJ Crowder you can multiply string by 1 and you get a number (numberOfCats*1).或者如 TJ Crowder 所述,您可以将字符串乘以 1 得到一个数字 (numberOfCats*1)。

Your second entry is a multiplication (*).您的第二个条目是乘法 (*)。 That can't be simply added as a string, so JS is responding with the answer to the multiplication you've entered.这不能简单地作为字符串添加,因此 JS 会以您输入的乘法答案作为响应。 The first entry is addition (+), which treats the string as a concatenation rather than equation.第一个条目是加法 (+),它将字符串视为连接而不是等式。

Javascript always concatenate with '+' operator if the input is in numeral.如果输入是数字,Javascript 始终与“+”运算符连接。 but with operators like *,/,% it always convert the string entered in numeral format to number.但是对于像 *,/,% 这样的运算符,它总是将以数字格式输入的字符串转换为数字。

5+'5'=55

5*'5' doesn't make any sense if Javascript teaches it as a string.如果 Javascript 将其作为字符串进行教学,则5*'5'没有任何意义。 Similarly for *,/,%. *,/,% 也类似。

In javascript "+" operator used for addition also "+" is overloaded as concatenation operator which is automatically decided based on the operands.在 javascript 中,用于加法的“+”运算符也重载了“+”作为连接运算符,它是根据操作数自动确定的。 If any of the operads is a string it will concatenated.如果任何操作数是一个字符串,它将被连接起来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM