Sub topic 5
Literals, operators, and type coercion
This chapter assumes you can read a workflow file and know that ${{ }} marks an expression. Here you will learn which values you can write inside an expression, which operators combine them, and how GitHub converts types when you compare them.
Literals
An expression can contain these literal values:
| Type | Examples |
|---|---|
| Boolean | `true`, `false` |
| Null | `null` |
| Number | `42`, `-9.2`, `0xff`, `-2.99e-2` |
| String | `'hello'`, `'it''s'` |
Strings must use single quotes. To put a single quote inside a string, double it, as in 'it''s'. Double quotes are not valid string delimiters in an expression.
Operators
| Operator | Meaning | Column | Column |
|---|---|---|---|
| `( )` | Grouping | ||
| `[ ]`, `.` | Index and property access | ||
| `!` | Not | ||
| `<`, `<=`, `>`, `>=` | Ordering | ||
| `==`, `!=` | Equality | ||
| `&&` | And | ||
| `\ | \ | ` | Or |
String comparison ignores case, so 'Main' == 'main' is true.
The && and || operators return one of their operands, not necessarily true or false. This makes || useful for defaults, because ${{ inputs.name || 'world' }} yields 'world' when the input is empty.
Type coercion
When the two sides of a comparison have different types, GitHub converts both to numbers.
| Value | As a number |
|---|---|
| `null` | `0` |
| `true` / `false` | `1` / `0` |
| String that looks like a number | That number |
| Empty string | `0` |
| Any other string, array, or object | `NaN` |
NaN is never equal to anything, including another NaN. Arrays and objects are equal only if they are the same instance.
For conditions and for !, &&, and ||, a value is converted to a boolean instead. The falsy values are false, 0, -0, '', and null. Everything else is truthy, including the string 'false' and any array or object.
Seeing it run
name: coercion
on: workflow_dispatch
jobs:
demo:
runs-on: ubuntu-latest
steps:
- name: Show results
env:
STRING_EQUALS_NUMBER: ${{ '1' == 1 }}
WORD_EQUALS_ZERO: ${{ 'abc' == 0 }}
NULL_EQUALS_ZERO: ${{ null == 0 }}
CASE_INSENSITIVE: ${{ 'Main' == 'main' }}
DEFAULT_VALUE: ${{ '' || 'fallback' }}
FALSE_STRING: ${{ 'false' && 'yes' || 'no' }}
run: |
echo "1 == 1: $STRING_EQUALS_NUMBER"
echo "abc == 0: $WORD_EQUALS_ZERO"
echo "null == 0: $NULL_EQUALS_ZERO"
echo "Main == main: $CASE_INSENSITIVE"
echo "default: $DEFAULT_VALUE"
echo "'false' &&: $FALSE_STRING"The job prints true, false, true, true, fallback, and yes. The last result surprises many people. The string 'false' is truthy, so && returns 'yes'.
Common errors
- Comparing a boolean input to a string. A
workflow_dispatchinput of type boolean is a real boolean in theinputscontext. Ingithub.event.inputs, it is the string'true'or'false'. Comparinginputs.flag == 'true'can still work, because both sides are converted to numbers. Writingif: github.event.inputs.flagis always true, because both strings are truthy. - Expecting
==to compare objects.github.event == github.eventis not a way to compare payloads. Compare specific properties instead. - Double quotes.
${{ github.ref == "refs/heads/main" }}is a syntax error. Use single quotes.
Exercise
What does ${{ 0 || null || 'x' }} evaluate to, and why? What does ${{ '0' == false }} evaluate to?
Show answer
The first expression yields 'x'. The values 0 and null are falsy, so || moves on to the last operand and returns it. The second expression is true. The string '0' and the boolean false are both converted to the number 0, and 0 == 0.