The string is an ordered sequence of 16-bit Unicode characters. The string is JavaScript data type for representing text. Each element in the string occupies a fixed position in it, these positions are called indexes. The first element is at index 0, second at index 1 and so on.
The length of string is the number of 16-bit values it contains. Usually one character corresponds to one 16-bit value, but some rarely used unicode characters are encoded as a sequence of two 16-bit values. That means that string with one character may have a length of 2. To read a string length use length property:
var s = "a"; console.log(s.length); //1
To concatenate strings use + operator on them:
var name = "Graham"; var city = "Dublin"; var text = name + " lives in " + city; console.log(text); //Graham lives in Dublin
String literals
To create string simply surround sequence of characters with double (“) or single (‘) quotes. There is no difference between them, just be consistent and use a pair of the same characters, otherwise an error will occur:
var language = 'JavaScript'; // correct var version = "ECMAScript 6"; // correct var api 'DOM API"; // SyntaxError
Escape sequences
The backslash character () has a special meaning in JavaScript strings. It allows to escape from the normal interpretation of a character that follows it.
For example to place an singe apostrophe character inside a string surrounded by single quotes (similarly for double apostrophes), you must use the backslash character to “escape” this apostrophe:
var text = 'Don't worry, I'm fine';
Backslash and the following character form a escape sequence that represents special characters. Below a list of several useful escape sequences:
- ‘ – single quote,
- ” = double quote,
- – backslash,
- n – new line,
- r – carriage return,
- t – tab.
Any character can be presented by specifying its latin one or unicode character code as a hexadecimal number:
xdd – a latin-1 character specified by the two hexadecimal digits (dd),
udddd – a unicode character specified by the four hexadecimal digits (dddd).
String – a primitive data type and object
Values of type string are not objects, and string is a primitive data type. However you can read property length from string type value, you can also invoke number of methods from string type value.
How does it happend? In fact JavaScript has both string primitive data type and its object representation. If needed, JavaScript interpreter temporarily converts string to a String object.
var s = "String data type"; //primitive string var char = s.charAt(7); // string is temporarily converted to object to call method console.log(char); // d
String immutability
Strings in JavaScript are immutable, it means that their values cannot be changed. Methods that manipulate the values of strings do not change their value, they return a new value.
String methods
See String methods.
Template literals
Template literals are string literals allowing you to embed expressions. You can use string interpolation, and multiline strings with them.
Template literals are enclosed by the back-tick characters (`) instead of single or double quotes.
var stringLiteral = `template literal`;
String interpolation
Template literals can include interpolated expressions in placeholders. They are marked by the dollar sign and curly braces (${expression}). Expressions are evaluated and parts are concatenated into a single string.
var name = 'Tom'; var prize = 'cinema tickets'; var interpolatedText = `Congratulations ${name}! You won ${prize}.`; console.log(interpolatedText); // Congratulations Tom! You won cinema tickets.
Multi-line strings
Template strings can stretch across multiple lines. Inserted newline characters are part of the template literal.
var multiLine = `This is example of multi-line string.`; console.log(multiLine); // This is example of // multi-line string.
Reply