What is the exclamation mark used for in code?

Q: What is the exclamation mark used for in code? What does the exclamation point mean?

A: In most cases the exclamation mark means “not”, “no”, “is not”, “does not”, “false”, “null”, or “undefined”. However, not every coding language follows this rule.

Below are some examples which may help you to further understand how the exclamation mark works in some types of code.

$four != $three

Four does not equal three.

4 !< 3

Four is not less than three.

if(!$variable){
// Do something
}

If $variable is false, null, undefined, do something. In other words, if no $variable, do something, otherwise, do nothing.

$variable = 1;
if(!$variable){
   // Do something
}

Unlike the previous example, $variable has a value, and therefore nothing would happen.

$variable = false;
if(!$variable){
   // Do something
}

Although $variable has a value, it’s value is false, and therefore would do something. It’s important to note that a value of 0 is the same as false, and would produce the same result.

if(!jQuery("#my_object").hasClass("my_class")){
   // Do something
}

If #my_object does not have the css class name .my_class, do something.

An exception to this rule is the CSS !important rule. The !important rule in CSS causes the defined style settings to override any others which may exist for the associated object.

#my_object{
 padding: 50px !important;
}

#my_object{
 padding: 10px;
}

In the above example, even though the padding for #my_object is lastly defined as 10px, it will still be 50px because it’s marked !important. The !important rule will override any others.