Strings are iterable collections
String data type became an iterable collection of data from the ES2015 standard. “iterables”. Strings are now accessible by all mechanisms that require the collection to have the “iterable protocol” implemented. So now we can use “for..of” over strings:
|
1 2 3 4 5 6 7 |
for (const clanKolekcije of 'abc') { console.log(clanKolekcije); } // Returns: // a // b // c |
or even use the spread operator which also requires an iterable collection to create an array:
|
1 2 |
const karakteri = [...'abc']; // console.log(karakteri); // Returns: ['a', 'b', 'c'] |
See more about this in “Iterators and the Iterable Protocol”
Template literals

Template literals (eng. “template literals”) are character strings (so-called strings) with built-in expressions. They are marked with backtick.
“Template literal” represents a new improved way of string concatenation, which is more transparent and easier to work with. Expressions are allowed to be embedded in the template literal, if they are marked with a dollar sign and curly brackets. The template includes the so-called ” “preset format” while “literal” represents a value that is written exactly as it is intended to be interpreted. Therefore, every newline, extra space (tab) becomes part of that string
Template literals combine the following characteristics:
- String interpolation
123let first = 'Pera';let last = 'Perić';console.log(`Hello ${first} ${last}!`); // Returns: Hi Pera Perić! - Multi-line string literals so called. “preset format”, ie. how it is written in the code is how it is interpreted at the output.
12345678910let multiLine = `This is itsome stringwhich is definedin multiple lines`;// Output:// This is it// some string// which is defined// in multiple lines
Example
Example done with ES5 and string concatenation:
|
1 2 3 4 5 6 7 8 |
var person = { firstName: 'Petar', nadimak: 'Pera', }; console.log('Hello, Ja sam ' + person.firstName + '!' + "n" + 'you can call me "' + person.nadimak + '".'); // Hello, Ja sam Petar! // You can call me "Pera". |
New approach with template literals:
|
1 2 3 4 5 6 7 8 9 |
var person = { firstName: 'Petar', nadimak: 'Pera', }; console.log(`Hi, I'm ${person.name}! You can call me "${person.nickname}".`); // Hello, Ja sam Petar! // You can call me "Pera". |
New string methods
String.startsWith()
This method checks if the string starts with the specified character set, if it does then returns “True”:
|
1 |
hello'.startsWith('hell') // Returns: True |
Or with an additional optional parameter defining the place:
|
1 |
'hello'.startsWith('ello', 1); // Returns: True |
String.endsWith()
This method checks if the string ends with the specified set of characters, if it does then returns “True”:
|
1 |
'hello'.endsWith('ello') // Returns: True |
Or with an additional optional parameter defining the place:
|
1 |
''hello'.endsWith('hell', 4); // Returns: True |
String.include()
This method checks if the string contains a certain set of characters, if it does then returns “True”:
|
1 |
'hello'.includes('ell') // Returns: True |
Or with an additional optional parameter defining the place:
|
1 |
'hello'.includes('ell', 1); // Returns: True |
String.repeat()
This method returns the selected string incremented by the desired number of times.
|
1 |
'doo '.repeat(3) // Returns: 'doo doo doo ' |
String.fromCodePoint()
This method returns a string of the specified numeric value.
|
1 2 3 4 5 |
var nekiString = String.fromCodePoint(65); console.log(nekiString); // "A" String.fromCodePoint(65, 90); // "AZ" String.fromCodePoint(194564); // "uD87EuDC04" |
See Online Unicode text converter, while the codes of each keycode can be viewed here.
String.codePointAt()
Returns a numeric value from the member at the specified position
|
1 |
'ABC'.codePointAt(1); // Returns: Unicode value za B tj. 66 |
