A string is a sequence of characters used to represent text. In JavaScript, any value placed between single or double quotes is considered a string. This means that 1
is a number, but "1"
is a string. When used with strings, typeof
returns the string "string"
:
> var s = "some characters"; > typeof s; "string" > var s = 'some characters and numbers 123 5.87'; > typeof s; "string"
Here's an example of a number used in the string context:
> var s = '1';
> typeof s;
"string"
If you put nothing in quotes, it's still a string (an empty string):
> var s = ""; typeof s;
"string"
As you already know, when you use the plus sign with two numbers, this is the arithmetic addition operation. However, if you use the plus sign with strings, this is a string concatenation operation, and it returns the two strings glued together:
> var s1 = "web"; > var s2 = "site"; > var s = s1 + s2; > s; "website" > typeof s; "string"
The dual purpose of the +
operator is a source...