Strings are a part of almost any object-oriented programming language. Today, we’re going to discuss some of the basics of strings, and what new features are available to us with strings when using ES6.


Strings are a sequence of characters and they are objects. These characters could be a sentence, a code, and many other things. JavaScript has its own way of creating strings. .In JavaScript, you can construct a string in three different ways; you can use double quotes (“Hello World”), single quotes (‘Hello World’), or with ES6, backticks (Hello World). One other thing to note is that you can combine strings with variables

In JavaScript, you can construct a string in three different ways; you can use double quotes (“Hello World”), single quotes (‘Hello World’) which are considered string literals, or with ES6, backticks (Hello World) , which are template literals. Strings also some interesting properties.

One property of strings is that you can combine them  with variables (“Hello World” + variable1). Not only that, you can also perform operations on strings because strings are objects. In this case, we’ll be going over mostly the new operations introduced in ES6 that  you can perform on strings.


The repeat() method allows you to repeat the output of the string. For example, if you had a string: “Hello World”, the repeat method would give you the output: “Hello WorldHello World”. This can be useful if you wanted to create a text repeater.


The includes() method searches a string for another string and returns true if it’s included in the string. This method is useful for searching for a particular word in a string, without using a regular expression to check for it.


The indexOf() method returns the index within the string of the first occurrence of another string, otherwise returns -1 if the value is not found. This isn’t a new feature of the language but can be useful for extracting a substring from another string or counting the occurrence of a substring within a string.


The startsWith() method returns true or false if the string starts with the specified sequence of characters. You can also change where in the string to check if the string starts with the specified sequence of characters. This can be useful if you want to check for a case sensitive sequence of characters at the start of your string.

Code Example

// Strings
//String Examples
//String Literals
//Double quotes
var string1 = "Hello World";
//Single quotes
var string2 = 'Hello World';
//Template Literals
var templateLiteralString = `Hello World`;
//Both String Literals and Template Literals can use string methods.
console.log(string1.repeat(2)); //Hello WorldHello World
console.log(string1.includes("Hello")); //True
console.log(string1.includes("World")); //True
console.log(string1.indexOf('H')); //0
console.log(string1.indexOf('W')); //6
console.log(string1.startsWith("Hello")); //True
console.log(string1.startsWith("World")); //False
view raw Strings.js hosted with ❤ by GitHub


Strings are something you’ll use often when dealing with game text or displaying information in the game for players to see; they will be a staple part of a lot of JavaScript code that you write. And, the string class has a lot of valuable methods that can make your life easier when dealing with strings. Template literals are extremely powerful for formatting strings, plus combining them with variables, and string literals are important for just showing basic text.  

As always, I hope that this post helps you understand strings a bit better, and improves your JavaScript code overall. If you have any suggestions or comments, please comment down below.

%d bloggers like this: