DEV Community

loading...

AutoHotkey Beginner Tip #1: Assignment Vs Expression Operator Usage

Ian Pride
Self taught programmer/scripter/coder/(web/graphic) designer in various languages for over 20 years. My Main GitHub IO site posted below is under construction with a new theme template in the works.
Updated on ・2 min read

Over the years I have helped many people with learning/debugging AutoHotkey in forums and one of the most common areas where people have issues (confusion) while learning AutoHotkey is with the difference between the assignment & expression operators '=' & ":=" respectively. Both can assign value, but the former is purely assignment; the latter is used for any expression (code to be executed/evaluated).

Assigning value with the assignment '=' operator

  • varName = Any unquoted string.
  • varName = %anotherVarName%

Assigning value with the expression ":=" operator

  • varName := "Any quoted string."
  • varName := anotherVarName
  • varName := !anotherVarName?0:255
  • varName := !anotherVarName?"Yes":"No"
  • varName := ((anotherVarName == "whatImTestingFor")?"Value exists":"Value doesn't exist")

Differences

  • =

    • Doesn't use quotation marks for strings.
    • Variable names must be wrapped with %%.
  • :=

    • Needs quotation marks for strings.
    • Variable names don't need to be wrapped.
    • Can contain any possible expression operation in the language.

Opinion

In my opinion there's only one use for '=' ever. :

  • '=' can be used to strip leading and trailing spaces from variables

    • E.g. varName := " String with spaces "
    • varName = %varName%
    • Result: varName == "String with spaces"

Everything else can always be done with ':='. That, of course, is your preference.

My Disclaimer

This is my first post here & I'm very aware of the hate for AHK by many so I will write my disclaimer here once.

Even though I know quite a few other languages I will always be an AutoHotkey advocate.

Many people do not like the chaotic syntax of AHK, but once you understand it & create your own consistent style it's actually quite nice (IMO?). I am aware of it's inadequecies in certain areas, but like many other languages it is still useful for many things (We can even plug in to UWP with Selenium now :D).

Discussion (4)

Collapse
coding_with_ju profile image
coding_with_ju

Thanks for clearing this up, I have pretty much always used := as it feels "correct" to put strings in quotes to me.

I didn't know people hated AHK that much, it might be because it was the first scripting language I picked up but I ❤ AHK

Collapse
thefluxapex profile image
Ian Pride Author • Edited

As I stated in my post it has some places that other tools do far better in so lots of people consider it useless and outdated and it also has a bad reputation as a cheating/hacking tool when that's just a small part of it. Hell the best game hacking tools are written in languages like C++ and assembly...

Collapse
coding_with_ju profile image
coding_with_ju

It's a shame it has that rep, as you say there are probably much better placed tools to do so. Outdated, maybe? But if it ain't broke don't fix it!

Thread Thread
thefluxapex profile image
Ian Pride Author

Outdated because the Windows environment is changing and more dependent on UWP in areas and AHK was built for win32 applications and the AHK devs don't really work on it much anymore. There is an AHK v2, but it's actually a bit different and not even as well maintained as V1. In order to use it with UWP you need Selenium so then it's not a stand-alone application. Not that big of an issue to me, but it is for many.

Forem Open with the Forem app