Javascript 为var指定默认值的条件
我可以这样做吗Javascript 为var指定默认值的条件,javascript,node.js,pug,Javascript,Node.js,Pug,我可以这样做吗 #{myVar || NULL} 玉器 如果myVar为空,如何显示NULL或其他内容?我试过上面的代码片段,它根本不显示任何内容。在Jade中,您可以像在javascript中一样使用条件语句 如果评估结果为falsy: - if (myVar) h1=myVar - else h1 Null 变量是用前导的hypen声明的 -myVar = null h1= "h1= '" + myVar + "' with myVar: '" + myVar + "'
#{myVar || NULL}
玉器
如果
myVar
为空,如何显示NULL
或其他内容
?我试过上面的代码片段,它根本不显示任何内容。在Jade中,您可以像在javascript中一样使用条件语句
如果评估结果为falsy:
- if (myVar)
h1=myVar
- else
h1 Null
变量是用前导的hypen声明的
-myVar = null
h1= "h1= '" + myVar + "' with myVar: '" + myVar + "'"
// displays: h1= 'null' with myVar: 'null'
-myVar = 'Hello lucky boy'
h1= "h1= '" + myVar + "' with myVar: '" + myVar + "'"
// displays: h1= 'Hello lucky boy' with myVar: 'Hello lucky boy'
-anotherVar = (myVar || null)
h1= "h1= '" + anotherVar + "' with myVar: '-anotherVar = (myVar || null)'"
// h1= 'Hello lucky boy' with myVar: '-anotherVar = (myVar || null)'
-myVar = null
if myVar
h1= 'myVar has content'
else
h1= 'something else'
// displays: something else
您可以通过将内容保存在“test.jade”文件中,运行jade test.jade
,然后在浏览器中查看test.html来尝试这些小测试
这个网站有一些很好的例子和一个互动的游乐场:
玩得开心。如果
NULL
是一个字符串,则需要将其括在引号中:
#{myVar || "NULL"}
类似地,您可以将其替换为任何其他字符串
#{myVar || "something else"}