What are the drawbacks of passing an array of usernames to a JavaScript function directly from PHP?

Passing an array of usernames directly from PHP to a JavaScript function can expose sensitive information and pose a security risk, as usernames can be easily accessed and manipulated by malicious users. To solve this issue, you can encode the array of usernames into a JSON format in PHP and then pass the encoded string to the JavaScript function.

<?php
$usernames = array("user1", "user2", "user3");
$usernames_json = json_encode($usernames);
?>

<script>
var usernames = <?php echo $usernames_json; ?>;
// Use the usernames array in JavaScript code
</script>