In JavaScript, if you want to print the string “null” when a variable is null
, you can do so add in !== operator.
Benefits of Using if-else Statements:
- Clarity: The code is easy to read and understand. It clearly communicates the intention: if the public key is present, display it; otherwise, display a default message.
- Error Avoidance: Handling null values prevents unexpected errors that might occur when trying to use null data.
- User-Friendly: Displaying a meaningful message like “No key available” instead of a blank space or undefined value enhances the user experience.
- Maintainability: The code can be easily maintained and updated, even by developers who didn’t originally write it, thanks to its readability.
Example:-
if (html.data.pubkey !== null) {
$('#key_name').html(html.data.pubkey);
} else {
$('#key_name').html('NULL');
}
2nd step go to blade page and use ID where you want to show the data
<span id="key_name"></span>
Output :-