When I write powershell scripts, I always use below method to check whether variable is null or not.
1 2 | if ($variable -eq $null) { } |
When I use VScode, VScode suggested me to put $null to left. Why?
Check below example:
1 2 3 4 5 6 7 8 9 10 11 12 | $object = @(1, $null, 2, $null) # "not safe" comparison with $null, perhaps a mistake if ($object -eq $null) { # -eq gets @($null, $null) which is evaluated to $true by if 'This is called.' } # safe comparison with $null if ($null -eq $object) { 'This is not called.' } |
So, the right way to compare is always put $null on the left side.
1 2 | if ($null-eq $variable) { } |