Jump to content

Recommended Posts

Posted (edited)

💡 Did You Know AutoIt’s Truthiness Rules Can Surprise You?

Or that empty strings are False but "0" (as a string) is True?

Or that Not $variable doesn’t work the way you might think?


🔍 Understanding Logical Expression Evaluation in AutoIt

When working with AutoIt, one of the subtle but important differences compared to languages like C, JavaScript, or Python is how logical expressions are evaluated.

👉 AutoIt does not have a dedicated Boolean type. Instead, it uses other values (strings, numbers, etc.) and interprets them as True or False.

This article breaks down AutoIt’s “truthiness rules” and explains what Not $variable really means.


1️⃣ No Boolean Type in AutoIt

Unlike many languages, AutoIt does not define a bool type. Instead:

  • Strings and numbers are interpreted as logical values.

  • A function or expression that “returns True/False” is really returning an integer (1 or 0).


2️⃣ Truthiness Rules

When AutoIt evaluates a value in a conditional (If, While, etc.):

  • "" (empty string) → False

  • 0 (number) → False

  • Any non-empty stringTrue

  • Any non-zero numberTrue

Examples:

If "" Then MsgBox(0, "Test", "This will NOT show") ; False
If "hello" Then MsgBox(0, "Test", "This WILL show") ; True

If 0 Then MsgBox(0, "Test", "This will NOT show") ; False
If -1 Then MsgBox(0, "Test", "This WILL show") ; True

3️⃣ What Does Not $variable Do?

The Not operator simply inverts the truthiness of a value.

ConsoleWrite(Not "" & @CRLF)      ; 1 (True)
ConsoleWrite(Not "abc" & @CRLF)   ; 0 (False)
ConsoleWrite(Not 0 & @CRLF)       ; 1 (True)
ConsoleWrite(Not 123 & @CRLF)     ; 0 (False)

👉 In plain:
Not $variable is like asking “is this variable empty or zero?”


4️⃣ Real Example: _PathFull UDF

In the standard UDF File.au3, the function _PathFull has this line:

If Not $sRelativePath Or $sRelativePath = "." Then Return $sBasePath

This handles two cases:

  • $sRelativePath is "" → return the base path.

  • $sRelativePath is "." → return the base path (current directory).

💡 This works because AutoIt treats an empty string as False.


5️⃣ Common Pitfalls

Pitfall 1: Expecting False to exist

Local $x = False
If $x Then MsgBox(0, "Result", "Will this show?")
; False is just 0, there’s no real Boolean type.

Pitfall 2: Forgetting that "0" (string) is True

If "0" Then MsgBox(0, "Result", "This WILL show!")
; Non-empty string = True
; But numeric 0 would be False.

Pitfall 3: Misunderstanding Not

Local $val = "hello"
If Not $val Then
    MsgBox(0, "Result", "Will this show?")
    ; Never runs because "hello" = True
EndIf

Key Takeaways

  • AutoIt has no Boolean type — just numbers and strings used as truth values.

  • "" (empty string) and 0 (numeric) = False.

  • Everything else = True.

  • "0" (string) = True, but 0 (number) = False.

  • Not $var = “is this variable empty or zero?”.


📝 Conclusion

Understanding AutoIt’s truthiness rules is essential for writing reliable conditions.

They make scripts concise, but can be surprising if you expect strict Boolean behavior.

👉 Pro Tip:
Use AutoIt’s implicit truthiness for quick checks, but when clarity matters, be explicit:

  • Use StringLen() to check if a string is empty.

  • Don't use IsNumber() if you don't understand it, read more here.

  • Use comparisons (=, <>) for precise logic.

That way, your intent is clear, and you avoid bugs caused by AutoIt’s flexible but tricky truth rules.


💬 Final Note:
If I’ve misunderstood or explained something incorrectly, please share your thoughts — I’d be happy to update and improve this article!

Edited by Trong
Posted

This article has so much value to it and you've summarized everything in a well organized way. Thank you for sharing.

I recently found out some of this stuff the hard way. Lots of trial and error until I understood what was going on. You've shared some great stuff here that will benefit me and many others.

Posted
Spoiler
2 hours ago, Trong said:

💡 Did You Know AutoIt Has No Boolean Type?

Or that empty strings are False but "0" (as a string) is True?

Or that Not $variable doesn’t work the way you might think?


🔍 Understanding Logical Expression Evaluation in AutoIt

When working with AutoIt, one of the subtle but important differences compared to languages like C, JavaScript, or Python is how logical expressions are evaluated.

👉 AutoIt does not have a dedicated Boolean type. Instead, it uses other values (strings, numbers, etc.) and interprets them as True or False.

This article breaks down AutoIt’s “truthiness rules” and explains what Not $variable really means.


1️⃣ No Boolean Type in AutoIt

Unlike many languages, AutoIt does not define a bool type. Instead:

  • Strings and numbers are interpreted as logical values.

  • A function or expression that “returns True/False” is really returning an integer (1 or 0).


2️⃣ Truthiness Rules

When AutoIt evaluates a value in a conditional (If, While, etc.):

  • "" (empty string) → False

  • 0 (number) → False

  • Any non-empty stringTrue

  • Any non-zero numberTrue

Examples:

If "" Then MsgBox(0, "Test", "This will NOT show") ; False
If "hello" Then MsgBox(0, "Test", "This WILL show") ; True

If 0 Then MsgBox(0, "Test", "This will NOT show") ; False
If -1 Then MsgBox(0, "Test", "This WILL show") ; True

3️⃣ What Does Not $variable Do?

The Not operator simply inverts the truthiness of a value.

ConsoleWrite(Not "" & @CRLF)      ; 1 (True)
ConsoleWrite(Not "abc" & @CRLF)   ; 0 (False)
ConsoleWrite(Not 0 & @CRLF)       ; 1 (True)
ConsoleWrite(Not 123 & @CRLF)     ; 0 (False)

👉 In plain:
Not $variable is like asking “is this variable empty or zero?”


4️⃣ Real Example: _PathFull UDF

In the standard UDF File.au3, the function _PathFull has this line:

If Not $sRelativePath Or $sRelativePath = "." Then Return $sBasePath

This handles two cases:

  • $sRelativePath is "" → return the base path.

  • $sRelativePath is "." → return the base path (current directory).

💡 This works because AutoIt treats an empty string as False.


5️⃣ Common Pitfalls

Pitfall 1: Expecting False to exist

Local $x = False
If $x Then MsgBox(0, "Result", "Will this show?")
; False is just 0, there’s no real Boolean type.

Pitfall 2: Forgetting that "0" (string) is True

If "0" Then MsgBox(0, "Result", "This WILL show!")
; Non-empty string = True
; But numeric 0 would be False.

Pitfall 3: Misunderstanding Not

Local $val = "hello"
If Not $val Then
    MsgBox(0, "Result", "Will this show?")
    ; Never runs because "hello" = True
EndIf

Key Takeaways

  • AutoIt has no Boolean type — just numbers and strings used as truth values.

  • "" (empty string) and 0 (numeric) = False.

  • Everything else = True.

  • "0" (string) = True, but 0 (number) = False.

  • Not $var = “is this variable empty or zero?”.


📝 Conclusion

Understanding AutoIt’s truthiness rules is essential for writing reliable conditions.

They make scripts concise, but can be surprising if you expect strict Boolean behavior.

👉 Pro Tip:
Use AutoIt’s implicit truthiness for quick checks, but when clarity matters, be explicit:

  • Use StringLen() to check if a string is empty.

  • Use IsNumber() to verify numeric values.

  • Use comparisons (=, <>) for precise logic.

That way, your intent is clear, and you avoid bugs caused by AutoIt’s flexible but tricky truth rules.


💬 Final Note:
If I’ve misunderstood or explained something incorrectly, please share your thoughts — I’d be happy to update and improve this article!

..this is epic. I'll keep it here for future reference :lol:

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Posted
9 hours ago, Trong said:

👉 AutoIt does not have a dedicated Boolean type. Instead, it uses other values (strings, numbers, etc.) and interprets them as True or False.

This article breaks down AutoIt’s “truthiness rules” and explains what Not $variable really means.


1️⃣ No Boolean Type in AutoIt

Unlike many languages, AutoIt does not define a bool type. Instead:

  • Strings and numbers are interpreted as logical values.

  • A function or expression that “returns True/False” is really returning an integer (1 or 0).

That's plain wrong. Please reconsider!

Local $v = 1=1
ConsoleWrite(VarGetType($v) & @LF)

 

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Posted (edited)
10 hours ago, Trong said:

👉 AutoIt does not have a dedicated Boolean type.

What exactly leads you to this conclusion?
This cannot be deduced from your following text.
All of this can be explained perfectly by the fact that there are specific rules for converting other data types to the Boolean data type.
There is nothing explicitly against the existence of a corresponding dedicated Boolean data type.
 

10 hours ago, Trong said:

A function or expression that “returns True/False” is really returning an integer (1 or 0).

Nope - if that were the case, they would behave exactly the same way - but they don't:

ConsoleWrite("compare with int type:  " & ("0" = retInt()  ? True : False) & @CRLF)
ConsoleWrite("compare with bool type: " & ("0" = retBool() ? True : False) & @CRLF)

Func retInt()
    Return 1
EndFunc

Func retBool()
    Return True
EndFunc

 

10 hours ago, Trong said:
  • "" (empty string) and 0 (numeric) = False.

  • Everything else = True.

The following are also converted to False during conversion to Boolean:

Global $aArray[1]
Global $mMap[] 

If $aArray Then MsgBox(0, "", "Array = True")
If $mMap Then MsgBox(0, "", "Map = True")
If ObjCreate("Scripting.Dictionary") Then MsgBox(0, "", "Object = True")
If Default Then MsgBox(0, "", "Default = True")
If Ptr(0x0) Then MsgBox(0, "", "Ptr(0) = True")
If DllStructCreate("CHAR[1]") Then MsgBox(0, "", "DllStruct = True")
If ConsoleWrite Then MsgBox(0, "", "Function pointer = True")

 

10 hours ago, Trong said:

how logical expressions are evaluated.

Basically, this discussion only touches superficially on logical expressions in AutoIt.
In fact, the behavior is only a consequence of the real topic: Implicit data type conversions in AutoIt:

AutoIt (like many other languages) is a dynamically typed language.
In other words, variables do not have a fixed data type; instead, this can be changed at will during the lifetime of the variable.
Nevertheless, variables can be linked together as desired using operators.
But internally, at the machine level, only identical data types can be linked together.
For example, addition is only possible between two Int32s and not between a Double and an Int32, etc.

Therefore, one task of the AutoIt interpreter in operations with multiple operands or when the operation requires a specific target data type is to align the different data types with each other.
AutoIt defines fixed rules for how certain data types are converted into others.
Integers, for example, are converted to floats by considering the value as the integer part of the resulting float.

How exactly certain data types are converted to the Boolean data type (yes, Boolean is explicitly listed as a separate data type) is described in part in the >>AutoIt help<<.
Specifically, it says the following, for example:

Quote

Strings and numbers can be used as Booleans. An empty string "" equals Boolean False as does the number 0. Any other number value will be equal to Boolean True

This explains exactly the behavior you described above.
And we're not even talking about logical expressions yet - it's just about implicit data type conversion.
If you keep in mind that logical operators and the if branch must have the expression as a Boolean, it quickly becomes clear that this is not actually about the behavior of logical expressions, but rather that they are only a consequence of the implicit data type conversion of other data types to the Boolean data type.

In summary: The behavior described above describes the effects of implicit conversion of other data types to the Boolean data type.
Everything described can be explained completely by this.
However, I do not see the slightest indication in the description that AutoIt should not have a dedicated boolean data type.

Edited by AspirinJunkie

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...