Strings are a type of primitive value rather than an object, however there is a wrapper object to work with when it comes to a string which can give the allusion that a string is a kind of object. The wrapper object of a String is loaded with all kinds of useful methods to help worth with a string value. In addition the String wrapper object is array like, so often many array prototype methods can be used with a string by way of something like the Function call prototype method.
There are also backticks that can be used as well. These can be used in the same way as quotes, but also allow for things like function calls.
Strings can often end up being the result of an expression with one or more operators. This can sometimes be an unintended result when respecting a number. When adding two strings together the result is another string, and when adding a string and a number together the result is also a string. Sometimes a method or property will supply a string where a number might be expected resulting in string concatenation where addition was expected.
One way to create a string from an array is ti use the Array.join method. This is one of the many Array prototype methods that can be used to join all elements together and return a string when dealing with an array of strings.
In addition to the Array.join method there is also the String.split method that can do the revers of this spiting a string into an array of strings.
There are a number of ways to create a string from an Object in general. There are many native methods to work with such as Object.keys, Array.map, and so forth. There is also for in loops and the JSON.stringify method that come to mind also just to name a few options.
In many projects this is often the typical way of going about getting a string that is the result of user input. It is generally a better idea to use input tags rather that using prompt.
To get the character length of a string there is the length property of a string. Also strings can be worked with as if they are array like objects as well. When this is the case the character length of the string can also be though of as the corresponding array length as well. So in this post I will be showing some examples of how to convert a string to an array and vice versa as well.
To get a character of a given index value from the left to the right of the string, one way is to do so the same way as if it where an array. There is also a Sting.charAt method as well in addition to a number of other ways.
Setting a char might prove to be a bit more complicated and may involve concatenation and the use of the String.slice prototype method.
There is the process of converting an array to a string and doing the inverse of that as well. Typically this is doe with the Array.join method. With Array.join just call the prototype method off of the array instance and pass a string that will be the string that will be placed between each element in the array, this can be an empty string if nothing is desired to be between elements
To convert a string back to an array there is the String.split method. The argument that is given to the String.split method is what is to be used as an indicator as to where the various points in a string are to be split into elements in an array. This value as well can be an empty string which will result in each character being a single element in the array.
The trim String prototype method can be used to remove any additional while space that may be at the beginning or end of a string.
If you are not familiar with regular expressions, it might be a good idea to look into them more at some point. Every now and then they do come in handy when it comes to doing anything complex with pattern detection with strings.
The String.replace prototype method is another very helpful and powerful method that can be used for advanced search and replace operations using regular expressions. The first argument is the regular expression and the second argument is what the pattern is to be replace with when found.
So there are string prototype methods of the wrapper object, and other methods in other prototypes than can be used to produce strings fro other objects. The wrapper object of a string is array like so it is often easy to use array methods with strings.