Strings
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 ...
Get Object-Oriented JavaScript - Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.