x
<html>
<head>
<title>Global Scope Example</title>
<script>
const AppState = {
isAuthenticated: false,
user: null
};
function login(user) {
AppState.isAuthenticated = true;
AppState.user = user;
updateUI();
}
function logout() {
AppState.isAuthenticated = false;
AppState.user = null;
updateUI();
}
function updateUI() {
if (AppState.isAuthenticated) {
document.getElementById('status').innerText = `Logged in as ${AppState.user}`;
} else {
document.getElementById('status').innerText = 'Not logged in';
}
}
</script>
</head>
<body>
<h1>Global Scope Example</h1>
<div id="status">Not logged in</div>
<button onclick="login('Alice')">Login as Alice</button>
<button onclick="logout()">Logout</button>
</body>
</html>