SoFunction
Updated on 2025-03-10

Brief introduction to the basic method of string creation in JavaScript

There are several ways to create a string. The easiest thing is to include a set of characters in quotes, which can be assigned to a string variable.

   var myStr = "Hello, String!";



You can include strings in double quotes or single quotes, but be careful that the pair of quotes that define strings must be the same and cannot be mixed.


Declarations like var myString = "Fluffy is a pretty cat.'; are illegal.


Two quotation marks are allowed to make some operations simple, such as embedding one into another:

 ("<img src='img/' height='30' width='100' alt="Logo">");

 

We created several strings in the above script, but essentially, they are not real string objects, to be precise, they are values ​​of string type. To create a string object, use the following statement: var strObj = new String("Hello, String!");


When viewing using the typeof operator, you will find that the above myStr type is string, while the above strObj type is object.
   
If you want to know the length of the string, use its length property:.


Use method for obtaining the character usage at the specified position of the string: (index);