By analogy with other languages, most programmers think of this as type conversion. That is, a Number is converted to a String or vice-versa. It is usually referred to as type coercion because in most cases neither you, nor the data, have any choice in the matter.
However, there is a better way to think of what is happening.
JavaScript doesn't have types in the same way that other languages do. It is as type-free as it is possible to get.
In other languages variables have an assigned type and they can only reference an object of a compatible type.
In JavaScript variables don't have a type and they can reference any object.
In this sense JavaScript doesn't support type.
It does recognize the primitive values – Number, String and Boolean and their associated wrapper objects. These are the only things that work with the standard operators and hence any object that is involved in an expression has to have a primitive value associated with it. When an object is used in an expression it provides its associated primitive value. If that value is the wrong type of primitive value then the system will convert it if it is possible.
You can think of this as type conversion but it really is a very degraded use of the term when you compare it to the complex type hierarchies that are found in class-based languages.
It is better regarded as representation conversion. If you have a String which stores "123" and you want to add 1 to it then you need to change the representation of the number from "123" to 123 and then add 1.
Some Strings are valid representations of numbers and all numbers have a valid String representation.
Objects With Custom Values
Final version in book
Comparison Operators
Final version in book
Functions in Expressions
Final version in book
The Object Expression Principle
Final version in book
Summary
JavaScript expressions combine objects to produce a final object.
As JavaScript’s operators all work with primitive types the final object is always a primitive type.
All objects have a value which is determined by evaluating the valueOf method.
In general, an object’s value has to be a primitive type if it is to be used in an expression.
The only ambiguous operator in JavaScript is the + operator which is either addition or concatenation. Which it is depends on its operands. If either operand returns a String then it is concatenation and the other operands also have to provide String values.
Every object also has a String value which is provided by the toString method.
If the valueOf method fails to provide a primitive type during the evaluation of an expression the toString method is called to see if it provides a primitive type. If neither provides a primitive type then a runtime error occurs.
If the valueOf method returns a Number and the expression needs a string the toString method of the Number object is called and not the original object’s toString method.
If valueOf returns a String and a Number is required then the String’s valueOf is used.
The == comparison operator also uses valueOf to obtain a primitive value to compare, but the === operator does not.
The === operator tests for equality of primitive values or equality of object reference.
Both operators test for equality of reference if the expressions being compared are simple references.
Anywhere you can use an object you can use an expression.
This is an extract from the book Just JavaScript by Ian Elliot.
Most books on JavaScript either compare it to the better known class based languages such as Java or C++ and even go on to show you how to make it look like the one of these.
Just JavaScript is an experiment in telling JavaScript's story "just as it is" without trying to apologise for its lack of class or some other feature. The broad features of the story are very clear but some of the small details may need working out along the way - hence the use of the term "experiment". Read on, but don't assume that you are just reading an account of Java, C++ or C# translated to JavaScript - you need to think about things in a new way.
Just JavaScript is a radical look at the language without apologies.