在 JavaScript 中,如果值为 null 或未定义,则替换该值?

javascriptweb developmentobject oriented programming更新于 2025/11/6 8:12:17

要如果值为 null 或未定义,则替换该值,您可以使用以下语法 −

var anyVariableName= null;
var anyVariableName= yourVariableName|| anyValue;

示例

var value = null;
console.log("The value ="+value)
var actualValue = value || "This is the Correct Value";
console.log("The value="+actualValue);

要运行上述程序,您需要使用以下命令 −

node fileName.js.

Here, my file name is demo56.js

输出

这将产生以下输出 −

PS C:\Users\Amit\JavaScript-code> node demo56.js
The value =null
The value=This is the Correct Value

相关文章


有用资源