Why does the expression "1" -eq 1
comes TRUE in powershell ? The first one is of string type and the second one is of integer type why is it treating them as equal.
CodePudding user response:
This is called implicit type conversion.
Powershell automatically perform a type conversion when a value of one type is used in a context that requires a different type.
A type conversion is performed when a value of one type is used in a context that requires a different type. If such a conversion happens automatically it is known as implicit conversion. (A common example of this is with some operators that need to convert one or more of the values designated by their operands.) Implicit conversion is permitted provided the sense of the source value is preserved, such as no loss of precision of a number when it is converted.
In addition to the official documentation, here is an excellent answer by mklement0 here with some additional examples and references: PowerShell Implicit Type Conversions - does it happen? Is it safe?