You can also use json_encode for more complex things like arrays:
$simple = 'simple string';
$complex = array('more', 'complex', 'object', array('foo', 'bar'));
var simple = '<?php echo $simple; ?>';
var complex = <?php echo json_encode($complex); ?>;
Using cookies for this is a very unsafe and unreliable way, as they are stored clientside and therefore open for any manipulation or won't even get accepted/saved. Don't use them for this type of interaction.
Hope this works!!
To know more about Java, It's recommended to join our Java Training in Chennai today.