Consider the following code:
!!('foo');
The negation operator uses the abstract operation ToBoolean
to perform a type conversion, but my question is - does this involve type coercion?
Consider the following code:
!!('foo');
The negation operator uses the abstract operation ToBoolean
to perform a type conversion, but my question is - does this involve type coercion?
!!(x) appears to return the same result as Boolean(x). You can see this for yourself by typing the following into the JavaScript console:
Boolean(false) === !!(false)
Boolean(0) === !!(0)
Boolean("") === !!("")
Boolean(null) === !!(null)
Boolean(undefined) === !!(undefined)
Boolean(NaN) === !!(NaN)
All over values are 'truthy' in JavaScript. Haven't checked every other available value in JavaScript; that could take some time. ;-)
According to wikipedia http://en.wikipedia.org/wiki/Type_conversion " the word coercion is used to denote an implicit conversion", so yes there is type coercion involved, since the conversion is enforced.
Alo take a look at the answer to this question What is the difference between casting and coercing?