Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

แทนที่ค่า null หรือ undefined ใน JavaScript

ใช้ตัวดำเนินการ OR (||) เพื่อกำหนดค่าเริ่มต้นเมื่อตัวแปรเป็น null หรือ undefined

ไวยากรณ์

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 (เช่น node demo56.js)

ผลลัพธ์

The value =null
The value=This is the Correct Value