Primitive Data Types, Arrays, Loops, and Conditions - Part 2 [con't]
typeof returns the string "string".
Here's an example of a number used in string context:
If you put nothing in quotes, it's still a string (an empty string):
string concatenationoperation and it returns the two strings glued together.
The dual function of the
+ operator can be a source of errors. Therefore, it is always best to make sure that all of the operands are strings if you intend to concatenate them, and are all numbers if you intend to add them. You will learn various ways to do so further in the chapter and the book.
When you use a number-like string as an operand in an arithmetic operation, the string is converted to a number behind the scenes. (This works for all operations except addition, because of addition's ambiguity)
A lazy way to convert any number-like string to a number is to multiply it by 1 (a better way is to use a function called
parseInt(), as you'll see in the next chapter):
If the conversion fails, you'll get
A lazy way to convert anything to a string is to concatenate it with an empty string.
Some strings that have a special meaning, as listed in the following table:
Escaping the escape:
||End of line||
||Carriage return||All these:
||Here's my name in Bulgarian written with Cyrillic characters:
There are some additional characters which are rarely used:
\v (vertical tab), and
\f (form feed).