JS & PHP file as follows
console.log(my_val_json);
<script async src="<?php echo get_template_directory_uri(); ?>/js/java.js"></script>
<?php $my_php_val = array( 'banana', 'orange'); ?>
<script type="text/javascript">
var my_val_json = '<?php echo json_encode($my_php_val); ?>' ;
</script>
Upon refreshing the page multiple times, i get the below error in my console log:
Uncaught ReferenceError: my_val_json is not defined
And sometimes this error also appears on the 1st load. Please advice as i am unable to find any answers online for 2 days now. Thanks!
** NOTE: when i change async to defer, there are no errors (so far) **