Discussion with a colleague, should $null
be on the left or right side of the check? Any examples of why this is important?
$abc = $null
$null -eq $abc
True
$abc -eq $null
True
All ok
$abc = 6,7,$null,8,9
$null -eq $abc
False
$abc -eq $null
*No output*
Can someone explain what is happening when an array is compared?
$null is an automatic variable in PowerShell used to represent NULL. You can assign it to variables, use it in comparisons and use it as a place holder for NULL in a collection. PowerShell treats $null as an object with a value of NULL. This is different than what you may expect if you come from another language.
Eq is a comparison operator that will test if one numeric or string expression is equal to another, Syntax expression -eq expression. Eq is a case-insensitive match and will ignore wildcards.
PowerShell's Less Than Comparison Operator -lt Just as -eq has an opposite in -ne, so -gt (greater than) has a mirror image in -lt (less than). In other scripting languages these would be represented by > and < symbols, however PowerShell uses the dash then an abbreviation of the comparison operator, thus -lt.
When performing comparison operations, PowerShell will evaluate the first operand the operation to determine if it is a collection or a scalar value. If it is a scalar, it will compare the operands to each other as whole objects.
2 -eq 1,2,$null,3
False
$null -eq 1,2,$null,3
False
4 -eq 1,2,$null,3
False
If it is a collection, it will iterate through the collection comparing each element to the second operand and return the value of the second operand if it finds a match.
1,2,$null,3 -eq 2
2
1,2,$null,3 -eq $null
<nothing appears, indicating returned value is $null>
1,2,3 -eq 4
<nothing appears, indicating returned value is $null>
The key difference between the second and third examples. Unless you know absolutely whether the second operand is always or never $null
, you can't trust the output of the comparison operation.
On a side note, if both operands are collections, -eq
returns $null
in every case, because you can't use -eq
to compare collections.
So to answer the question, it is generally good practice to put $null
on the left side because when using $null
in a comparison operation, it is assumed that you want to compare scalar values. If that assumption is wrong, then $null
may be better placed on the opposite side, but it's not likely.
Contrarily, I always put $null
on the right side because I write my code under the aforementioned assumption - that I am always comparing scalar values when $null
is explicitly mentioned in the comparison operation. That way, when I get a non-boolean value returned, I know that I have not accessed my collection object properly in the equation. I have gotten caught in the trap, however, so my coding practices may be better served by changing my ways.
In conclusion, like any coding practice, it's a matter of opinion. Were I to teach PowerShell, I would teach this practice. However, I find it unlikely I will change my seditious ways.
-eq is different with arrays on the left. A lot of operators are.
1,2,3 -eq 2
Output:
2
So instead of 2, in your case it's returning $null, because $null is in the array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With