When handling null or undefined values, use modern JavaScript patterns to write more robust and maintainable code: 1. Use the nullish coalescing operator (`??`) instead of logical OR (`||`) for defaults:
When handling null or undefined values, use modern JavaScript patterns to write more robust and maintainable code:
??
) instead of logical OR (||
) for defaults:
```javascript
// Good: Only falls back when value is null or undefined
this.endpointUrl = fields.endpointUrl ?? getEnvironmentVariable(“AZUREML_URL”);// Avoid: Will also replace empty strings, 0, and false this.endpointUrl = fields.endpointUrl || getEnvironmentVariable(“AZUREML_URL”);
2. Conditionally include properties in objects only when they exist:
```javascript
// Good: Only adds the property when content exists
const options = {
...otherOptions,
...(message.content != null ? { content: formatContent(message.content) } : {})
};
// Avoid: Passing undefined values to APIs
const options = {
...otherOptions,
audio: this.audio // might be undefined
};
===
, !==
) for null checks:
// Good: Clear intent, checks for both null and undefined
if (documents === null || documents === undefined) {
// Handle the case
}
// Or more concisely:
if (documents == null) {
// Handle the case
}
// Avoid: Oversimplified check if (!config.client && !endpoint && !key) { // This will only throw if ALL are missing } ```
These patterns help prevent subtle bugs and make your code’s intent clearer to other developers.
Enter the URL of a public GitHub repository