The var
statement sets the scope of a variable to the function it is in.
You are trying to access a local variable in the global scope—where it doesn't exist.
Additionally, you don't set the variable until a deviceorientation
occurs, but you try to read it all the time.
You would have to rewrite your code like this:
<script>
window.addEventListener('deviceorientation', function(event) {
var a = event.alpha;
var b = event.beta;
var g = event.gamma;
console.log( g.toString() ); // Better to use a non-blocking method like console.log to display results.
}, false);
</script>