html - JavaScript runtime error: 'variable' is undefined while checking to see if undefined - Stack Overflow

I have the following lines written with HTML and some inline JS:<% if (x !== undefined || x !== null

I have the following lines written with HTML and some inline JS:

 <% if (x !== undefined || x !== null) { %>
  <div> Foo </div>
 <% } %>

It produces this dynamic function code:

if (x !== undefined || x !== null) {...

As well as this error:

0x800a1391 - JavaScript runtime error: 'x' is undefined

Can anyone explain why this is happening?

I have the following lines written with HTML and some inline JS:

 <% if (x !== undefined || x !== null) { %>
  <div> Foo </div>
 <% } %>

It produces this dynamic function code:

if (x !== undefined || x !== null) {...

As well as this error:

0x800a1391 - JavaScript runtime error: 'x' is undefined

Can anyone explain why this is happening?

Share Improve this question asked Aug 10, 2016 at 21:01 SimonSimon 86510 silver badges24 bronze badges 2
  • Check typeof x == "undefined" instead. – Sampson Commented Aug 10, 2016 at 21:03
  • 1 It happens because...x is not defined. JS has few layers of this, but the error messages aren't always clear on the distinction var x declares a variable and its value is undefined. So, you'll be able to use it because "they exist", so to speak. Undeclared variables tend to throw errors such as the one you saw because you are trying to access something that doesn't exist (undeclared). Although x = 5 will implicitly set a window.x in non-strict mode. – VLAZ Commented Aug 10, 2016 at 21:07
Add a ment  | 

5 Answers 5

Reset to default 2

In order for Javascript to pare the value of the x variable it must look it up; since it is not yet defined it throws an error message. This error is happening before the runtime even attempts to pare the value to undefined. It's a little bit of a chicken-and-egg problem.

use typeof x === 'undefined' instead.

It's because you're trying to access a variable that has never been defined.

Example:

'use strict';
console.log(x);

You can check if a variable has been declared using the typeof operator:

'use strict';
console.log(typeof x === 'undefined');

Not totally sure what syntax <% [some javascript] %> is (Classic ASP?), but as an alternative, sniff if x exists on your global object.

Since you've tagged this html, your global object should be window. (In node, as an example, the global object is literally global.)

<% if (window.x) { %>
  <div> Foo </div>
<% } %>

And you're done.

You could also use the more verbose, but also more precise, code I think you intended in your post, so that if x is falsy but not null or undefined -- eg, 0 or "" -- it still trips the if.

Though I'm pretty sure you wanted an && in there, not an ||. That is, as originally written, your condition will always evaluate true. If x is undefined, then it's by definition not null, and vice versa!

Here's the tweaked version...

<% if (window.x !== undefined && window.x !== null) { %>
  <div> Foo </div>
<% } %>

variable x is not defined in your javascript code. Do a check with typeof operator.

typeof x=="undefined"

if if returns true then your variable x is not defined.

try this

<% if (!x) { %>
  <div> Foo </div>
<% } %>

!x will return true for an empty string, NaN, null, undefined.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744330987a4568886.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信