First of all, the understanding of undefined. What is undefined ?
Solution:
Within <head> section of html append laravel csrfToken as javascript object.
<html>
<head>
<script>
// rename myToken as you like
window.myToken = <?php echo json_encode([
'csrfToken' => csrf_token(),
]); ?>
</script>
</head>
</html>
Test it on .js file,
console.log(myToken.csrfToken) // should be returning laravel csrf value