Variables in JavaScript technically have no fixed data type, as JavaScript is a dynamically typed language; meaning, we do not have to specify the data type of a variable when we declare it, and data types are converted automatically as needed during script execution. For example, we could initially define a variable with a string value as follows:
var answer = "This is a string ..."
and later, assign the same variable any of the recognized value types, such as a number.
answer = 123
This change in data type assignment does not cause an error message, unlike in other programming languages.
It is possible, under some circumstances, to force the automatic conversion, or coercion, of the value of a variable into a different type. Numbers can easily be included in strings.
In expressions involving numeric and string values with the + operator, JavaScript converts numeric values into strings. For example,
x = "The answer is " + 42 // returns "The answer is 42"
y = 42 + " is the answer" // returns "42 is the answer"
var start = 1;
var end = 10;
var doWhatever = "Count from ";
doWhatever += start + " to " + end + ".";
After this code is executed, our doWhatever variable will contain the string "Count from 1 to 10." The numbers have been coerced into string form.
Let's explore the statements to see how. First we declared three variables: start, end, and doWhatever. The variables start and end contains numbers, while doWhatever contains a string.
The += operator in the fourth statement means "join with", meaning that the current value of doWhatever will be joined with the result of the expression that follows the += sign, with the end result becoming the new value of doWhatever.
In statements involving other operators, JavaScript does not convert numeric values to strings. For example,
"37" - 7 // returns 30
"37" + 7 // returns 377
var next = 0;
next += 1 + "10";
In this case, because "10" is a string, the += operator concatenates the string elements. After this code is executed, the next variable contains "0110".
Let's explore the statements. First we declared the variable next with the value 0. The second statement joins our variable next with the result of the expression after the += operator. The expression 1 + "10" results in the string "110", as the number 1 is coerced into the string "1" and joined with the string "10".
In the same manner, the value of next is coerced into the string "0" and concatenated with the result of the expression which is "110", and results in "0110".
In performing math operations, strings cannot be included directly with numbers. They must first be converted into numeric data. JavaScript provides two explicit conversion functions for this purpose, parseInt ( ) and
parseFloat ( ).
var addStr = 0;
addStr += 1 + parseInt ( "10" );
In this case, the function parseInt ( ) converts the string "10" into an integer, and since all values are numeric, the += here performs addition. After this code is executed, our addStr variable contains the integer 11.