In JavaScript, the type of a value may be promoted or demoted (coerced) from one kind to another according to the context in which it is used.
A number becomes a string if it is used in a string concatenation.
A string becomes a number if it contains a numeric value and if it is used in an arithmetic expression.
This happens automatically as expressions are evaluated.
The specific behavior depends on the data type of the value at the time the expression is invoked. It also depends on the type of expression being invoked.
This also affects the values yielded by expressions when they are used as operands in other expressions. In this case the value is an ephemeral item not stored in a variable nor in a visible container that the script can access; nevertheless, it behaves as any other value would.
The result of a function call may be affected in the same way when it is used in an expression.
Refer to the descriptions of each of the primitive value types for details of the result of a type conversion on a value of that type.
Prev | Home | Next |
implements | Up | ImplicitParents |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |