A brief introduction to the basic method of string creation in JavaScript
- 2020-07-21 06:52:19
- OfStack
There are several ways to create a string. The simplest is to enclose a set of characters in quotation marks, which you can assign to a string variable.
var myStr = "Hello, String!";
You can include the string in double or single quotes, but note that the 1 pair of quotes that define the string must be the same and cannot be mixed.
var myString = "Fluffy is a pretty cat.'; Such a statement is illegal.
Two quotes are allowed, making it easy to do things like embed one in the other:
document.write("<img src='img/logo.jpg' height='30' width='100' alt="Logo">");
We created a few strings in the above script, but they are not really string objects per se; rather, they are string-type values. To create a string object, use the following statement: var strObj = new String("Hello, String!" );
Looking at the typeof operator, you will see that the above myStr is of type string and strObj of type object.
If you want to know the length of a string, use its length attribute: string.length.
string. charAt(index); string. charAt(index);