How can JSON encoding be utilized to pass data from PHP to JavaScript efficiently?

To pass data from PHP to JavaScript efficiently, JSON encoding can be utilized. This involves encoding PHP data structures into a JSON format that can easily be parsed by JavaScript. This allows for seamless transfer of data between the two languages.

<?php
$data = array(
    'name' => 'John Doe',
    'age' => 30,
    'city' => 'New York'
);

$json_data = json_encode($data);
echo "<script>var jsonData = $json_data;</script>";
?>