[英]What means '?' in Javascript?
Can anyone explain me this line of code and why we use the '?' 谁能向我解释这一行代码以及为什么我们使用“?” in javascript?
在JavaScript中?
return n > 0 ? ninja.yell(n-1) + "a" : "hiy";
This is a ternary operator which also present in other programming languages: 这是一个三元运算符 ,它也以其他编程语言提供:
return n > 0 ? ninja.yell(n-1) + "a" : "hiy";
^^ ^^ ^^
if condition if true if false(else)
The above statement is equivalent to below: 上面的语句等效于以下内容:
if(n>0) {
return ninja.yell(n-1) + "a";
} else {
return "hiy";
}
For more read this tutorial . 有关更多信息,请阅读本教程 。
The question mark is actually called Ternary Operator , usually in programming laguages it is used for a one line if statement and it has the following construct: 问号实际上称为三元运算符 ,通常在编程语言中用于一行if语句 ,它具有以下构造:
condition ?
条件? return if condition is True : return if condition is False
如果条件为True,则返回:如果条件为False,则返回
Think the ternary operator as " then " and the " : " as else . 将三元运算符视为“ then ”, 否则认为“ : ”。 So your code will be:
因此,您的代码将是:
return if( n > 0) then ninja.yell(n-1) + "a" else "hiy";
Hope you get it now! 希望你现在得到它!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.