0

So, this is apparently not allowed in Javascript today?

<script>
    if (fbq == null) { 
        fbq('track', 'ViewContent'); 
    }
</script>

Console returns this: uncaught reference error And also this in the web inspector: more detail

I thought this was pretty standard code? Surely?

Ignore the fact that fbq is undefined when called on line 142. It doesn't even get there. The error happens on line 141. I've tried testing for "typeof fbq" etc, and always returns the undefined error. Bizarre.

8
  • 4
    No, you can't use names that aren't declared. That's not new. You can test for object properties like window.fbq. Commented Dec 11, 2018 at 4:59
  • Have You defined it ? like this var fbq = ""; Commented Dec 11, 2018 at 5:01
  • The necessity of checking if a variable was ever declared seems like a code smell, although not sure if this was practical or just experimental. Commented Dec 11, 2018 at 5:04
  • @MarkMeyer what? You're saying " if (someUndeclaredVariable) {} " is not permitted? Commented Dec 11, 2018 at 5:04
  • Correct @3Dom, that's a ReferenceError Commented Dec 11, 2018 at 5:05

4 Answers 4

4

You can simply do:

typeof fbq || fbq === null // undefined

The typeof operator, unlike the other operators, doesn't throw a ReferenceError exception when used with an undeclared variable.

Sign up to request clarification or add additional context in comments.

1 Comment

For anyone interested in some additional info: "typeof returns a defined value for undeclared variables, before reaching the GetValue algorithm which throws for undeclared variables." - This is an oldie but goodie, and though it's dated, I believe all said in the answer still rings true these days.
0

Try this,

if (typeof(fbq) =='undefined' || fbq == null) { 
        fbq('track', 'ViewContent'); 
    }

As mentioned by @sanjay typeof operator doesn't throw ReferenceError exception when used on undefined variable.

Comments

0

From Mozilla:

The value null is written with a literal: null. null is not an identifier for a property of the global object, like undefined can be. Instead, null expresses a lack of identification, indicating that a variable points to no object. In APIs, null is often retrieved in a place where an object can be expected but no object is relevant.

// foo does not exist. It is not defined and has never been initialized:
foo;
"ReferenceError: foo is not defined"

// foo is known to exist now but it has no type or value:
var foo = null; 
foo;
"null"

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null

I believe that's how it behaves. If you have a look at the spec, it says:

7.2.12 Abstract Equality Comparison

The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows:

If x is null and y is undefined, return true.
If x is undefined and y is null, return true.
Return false.

http://www.ecma-international.org/ecma-262/6.0/#sec-abstract-equality-comparison

So basically if you try something like:

undefined == null // this should return true.

But you cannot use a reference which you have not defined. In my opinion, a good test to see if the variable is defined is to do something like below.

typeof variable !== 'undefined'

Feel free to ask if you need further clarifications :)

Comments

0

I swear I was trying permutations of this earlier and it was also throwing the same error, but isn't now? Weird stuff going on in my browser for the last hour. Very frustrating, but this works:

<script>
    if (!(typeof fbq || fbq === null)) { 
        fbq('track', 'ViewContent'); 
    }
</script>

Just updated the script to use Sanjay's code instead of mine (which also worked).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.