JavaScript Noob javascript 1 line explication

What does the following line do?

(flag)?flag=false:flag=true;

In this piece of code:

  var flag = true;
    function resize() {
        if(flag) {
            document.getElementById("centerimg").style.width = "150%";
            document.getElementById("centerimg").style.height = "150%";
            document.getElementById("center").style.left = 120+"px";
        } else {
            document.getElementById("centerimg").style.width = "100%";
            document.getElementById("centerimg").style.height = "100%";
            document.getElementById("center").style.left = 190.5+"px";
        }
        (flag)?flag=false:flag=true;
    } 
Answer:1

It is using what is called the ternary operator. It follows the same logic as an if-else statement, just a more condensed version. Standard format is condition ? true block : false block

The (flag) is checking a boolean condition - in this case it is seeing if flag evaluates to true. Between the ? and : is the action to perform if the boolean condition is true - setting flag to false in this case. The section after the : is what happens if the condition was false - setting flag to true.

So if we write this longhand it ends up as

if (flag) {
    flag = false;
} else {
    flag = true
}

Which can be further shortened into flag = !flag (flag is equal to the opposite of flag)

Answer:2

It's the same as if you wrote:

if(flag){
    flag = false;
}else{
    flag = true;
}
Answer:3

Ask if the flag is true, if flag is true, then change it to false, if the flag is false, change it to true.

Answer:4

a?a=true:a = false is CONDITIONAL OPERATOR

It is used to set the value of a to b or c depending on conditions.

In your case , it does the same thing as flag = !flag

Answer:5

Is there in the world any compiler for a simple functional language such as that defined on this article, or maybe just Scheme or even ML for itself? That is, a compiler that will analyze the code for ...

Is there in the world any compiler for a simple functional language such as that defined on this article, or maybe just Scheme or even ML for itself? That is, a compiler that will analyze the code for ...

I'm working on a site for a client where he needs a countdown timer to count down to 10pm two days from each site update. (or however many days he wishes to enter) The countdown functions perfectly in ...

I'm working on a site for a client where he needs a countdown timer to count down to 10pm two days from each site update. (or however many days he wishes to enter) The countdown functions perfectly in ...

I am using linux and phantomjs to test some javascript var page = require('webpage').create(); var fs = require('fs'); var address = system.args[1]; page.onConsoleMessage = ...

I am using linux and phantomjs to test some javascript var page = require('webpage').create(); var fs = require('fs'); var address = system.args[1]; page.onConsoleMessage = ...

  1. phantomjs command line example
  2. phantomjs command line arguments
  3. phantomjs command line
  4. phantomjs command line args
  5. phantomjs command line options
  6. phantomjs command line parameters
  7. phantomjs command line open url
  8. phantomjs command line pdf
  9. phantomjs command line timeout
  10. phantomjs.exe command line
  11. install phantomjs command line
  12. phantomjs linux command line
  13. exit phantomjs command line
  14. phantomjs windows command line
  15. phantomjs screenshot command line
  16. using phantomjs command line
  17. phantomjs proxy command line
  18. phantomjs config command line
  19. phantomjs pass arguments command line
  20. phantomjs run from command line

I have this Jquery function to mask my textbox: jQuery(function($){ //Mask textbox #hour with placeholder $("#hour").mask("9:99",{placeholder:"0"}); $("#hourInTable").mask("9:99",{placeholder:...

I have this Jquery function to mask my textbox: jQuery(function($){ //Mask textbox #hour with placeholder $("#hour").mask("9:99",{placeholder:"0"}); $("#hourInTable").mask("9:99",{placeholder:...

  1. jquery code doesn't work
  2. jquery-code-doesn't-work-on-dynamic-content-loaded-with-ajax