Q:

Control flow pattern when checking for nonexistent variables

I saw the following pattern and tried adapting it to my own use:
var request = window.ActiveXObject ?
  new ActiveXObject('Microsoft.XMLHTTP') :
  new XMLHttpRequest;

Simplifying:
var request = window.ActiveXObject ?
  "is ActiveX" : "not ActiveX";
document.write(request + "<br>");
// I get not ActiveX (Chrome)

Now when I try:
var notX = "x"; 
  var j = x ? "has x" : "no x";
  document.write(j + "<br>");
  // console error: x is not defined

http://jsfiddle.net/4mn8n/1/
Nothing gets outputted. What makes window.ActiveXObject different from x that the program continues to document.write but not in the case of x?
-- More Importantly
What's a robust pattern I can use to replace the above?

A:

When you check for an undefined variable just by using it as-is, that's an error. The original code has no error because it's accessing it as a property of the window object; fetching undefined properties, instead of being an error, results in undefined in JavaScript.
To check for variables, you can use typeof instead.
var j = typeof x !== 'undefined' ? "has x" : "no x";

