Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为var指定默认值的条件_Javascript_Node.js_Pug - Fatal编程技术网

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"}