Search code examples
javascriptactionscript-3if-statementboolean-logiccomma-operator

AS3/JavaScript if statement commas instead of & &


This runs in ActionScript 3 and JavaScript. Why? I know how && and || work, but a list? Is this AS3 specific? Is this in other languages? I'm a mouth breathing PHP/AS2 programmer. Or did everyone already know this and I'm a tool for not reading documentation properly?

AS3

if (true, true, true) {
     trace("true?")
}
//result - "true?" traced

JavaScript

if (true, true, true) {
    alert("true?");
}
//result - "true?" alert message popped up

if (false, false, false) {
    alert("true?");
}
else {
    alert("false");
}
//result - "false" alert message popped up

if(true, false, false) {
    alert("true?");
}
else {
    alert("false");
}
//result - "false" alert message popped up

Solution

  • I presume JavaScript has a comma operator like C, which takes multiple arguments and returns the last one. It's typically used to for loops where you want to initialize more than one value:

    for(i=0, j=0; j< 10; j++) {  
    ...   
    }