Introduction
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
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.
Repeat
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.
Includes
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.
IndexOf
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.
StartsWith
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
Conclusion
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.