When utilizing twig to declare a variable called user:
<script type="text/javascript>
{% if user is defined %}
var user = {
example: {{ userjson | raw }}
};
{% endif %}
</script>
If a user is not logged in, an error message appears on the console:
Uncaught ReferenceError: user is not defined
I am looking for a way to show a message when the user is not defined. Currently, I have the following setup:
if(user){
console.log('hello');
} else {
console.log('undefined');
}
// This code checks the user information
var userId = user.example.id;
console.log(user.example.money);