In cases where you have a known condition that you want to save as a cookie to the user's browser (Have they been here before? What was the last page they saw? Do they prefer mobile?), you can use the $response object to set parameters without needing to use JavaScript. This should be done before any HTML is output to the browser.
$response.setHeader("Set-Cookie","testcookie=lastPageVisited;expires=Sat, 01-Jan-2013 00:00:00 GMT;path=/;domain=yoursite.com")