As a one-liner:
fetch(location.href).then(response => console.log(response.status));
This is asynchronous, if you need a synchronous solution use XMLHttpRequest
(as in the other answer) together with async: false or use async/await
which feels synchronous, but is still asynchronous under the hood.
Attention: There is the potential risk that the status code of this new request isn't the same as the status code of the original page request, for several reasons, like timouts, authentication and so on.
Alternatively
An approach without an extra call would require to include the status code in the page on the server side, then read it on the client side via JavaScript, like so:
Java + Thymeleaf + meta tag:
<meta name="statuscode" th:content="${#response.status}">
Java + Thymeleaf + JavaScript variable:
<script th:inline="javascript">
const statusCode = [[${#response.status}]];
</script>
PHP + meta tag (unverified):
<meta name="statuscode" content="<?php echo http_response_code() ?>">