4 Replies - 479 Views - Last Post: 13 March 2012 - 11:15 AM

#1 black_yurizan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 89
  • Joined: 18-June 11

Quick question about if statement

Posted 10 March 2012 - 07:03 AM

Hello, if the if statement has a statement with no conditions, like


if(document.getElementById)
{//do something
}





Does it mean that whenever document.getElementById is used at anytime it will do the if statement?
Is This A Good Question/Topic? 0
  • +

Replies To: Quick question about if statement

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3572
  • View blog
  • Posts: 10,414
  • Joined: 08-June 10

Re: Quick question about if statement

Posted 10 March 2012 - 07:11 AM

that wouldn’t qualify as "no contition". the above code is about the same as
if ("getElementById" in document) {
    // …
}

Was This Post Helpful? 0
  • +
  • -

#3 black_yurizan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 89
  • Joined: 18-June 11

Re: Quick question about if statement

Posted 10 March 2012 - 07:43 AM

Well if the condition is not equivalent(==) or not equal to (!=), then what is it telling to the if statement when its alone by itself
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3572
  • View blog
  • Posts: 10,414
  • Joined: 08-June 10

Re: Quick question about if statement

Posted 10 March 2012 - 07:52 AM

a condition doesn’t need a comparison operator. all it does is evaluate the given expression.
Was This Post Helpful? 0
  • +
  • -

#5 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: Quick question about if statement

Posted 13 March 2012 - 11:15 AM

black, "if (whatever)" is the same as saying "if (whatever) == true". So, for example:
var x=1;
var y=0;
if (x) {alert ('foo')];
if (y) {alert ('bar')};

will alert "foo" but not "bar", because javascript will cast the 1 to true and the 0 to false.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1