3

Possible Duplicate:
How do you read this JavaScript code? (var1 ? var2:var3)
JS How to use the ?: (ternary) operator

I was looking at a website's code and found this:

$.body = $('body');    
$.scroll = ($.browser.mozilla || $.browser.msie) ? $('html') : $.body;

What is the second line saying? Looks like some sort of if statement

Thanks

5
  • 6
    It is the JavaScript ternary operator: stackoverflow.com/questions/1788917/javascript-ternary-operator Commented Jan 20, 2012 at 21:11
  • @JustinEthier Might as well post that as an answer Commented Jan 20, 2012 at 21:13
  • Thanks for the link @JustinEthier - I didn't know what the term was called. Commented Jan 20, 2012 at 21:17
  • @Chad - I was going to, but questions about the ternary operator seem to come up so frequently that this question will probably end up being closed... Commented Jan 20, 2012 at 21:23
  • @user906080 - No worries, it's basically impossible to search for ? unless you already know the operator by name... Commented Jan 20, 2012 at 21:24

2 Answers 2

3

If the browser is mozilla, or if the browser is msie, then select the html dom object, else, select the body.

var a = CONDITION ? IF_TRUE : IF_FALSE;
Sign up to request clarification or add additional context in comments.

Comments

3

It could have also been written as (but most people would prefer the style that you posted):

if ($.browser.mozilla || $.browser.msie) {
    $.scroll = $('html');
} else {
    $.scroll = $.body;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.