Definition of "!"

Back to topics list To post a new topic, please log in or register
avatar
41
KeepMarcos 2016.02.18 00:33
 

Hello people!!

Why some programmers put the "!" before a function ??

example:

!isPositionOpen() and isPositionOpen()

What do the different "!" ??

 

thanks for help! =]

avatar
2
nodejs787 2016.02.18 02:05 #
 

bool SampleFunction()
{
    return(true);
}

// The "!" operator is used to negate the value of a function. If a function returns "true", it will make it "false" and vice-versa.
// Example

if(SampleFunction() == true)
{
    Alert("TRUE");
} else {
    Alert("FALSE");
}
// Alerts "TRUE".

if(!SampleFunction() == true)
{
    Alert("TRUE");
} else {
    Alert("FALSE");
}
// Alerts "False".


avatar
12983
WHRoeder 2016.02.18 02:36 #
 
KeepMarcos: Why some programmers put the "!" before a function ??
You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So Don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled. Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.
avatar
2
nodejs787 2016.02.18 19:44 #
 
WHRoeder:
KeepMarcos: Why some programmers put the "!" before a function ??
You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So Don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled. Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.
Since the OP did not know what the "!" meant, I used the example above to make the point extra clear and obvious, however, your clarification is correct. Hopefully the OP now understands what the "!" operator does and is not further confused by your clarification which does not answer OP's question. Cheers
Back to topics list  

To add comments, please log in or register