- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Replace a value if null or undefined in JavaScript?
To replace a value if null or undefined, you can use below syntax −
var anyVariableName= null; var anyVariableName= yourVariableName|| anyValue;
Example
var value = null; console.log("The value ="+value) var actualValue = value || "This is the Correct Value"; console.log("The value="+actualValue);
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo56.js
Output
This will produce the following output −
PS C:\Users\Amit\JavaScript-code> node demo56.js The value =null The value=This is the Correct Value
Advertisements