Text formatting | javascript string format
TheString[1]objectisawrapperaroundthestringprimitivedatatype.constfoo=newString(foo);console.log(foo);typeoffoo;YoucancallanyofthemethodsoftheStringobjectonastringliteralvalue—JavaScriptautomaticallyconvertsthestringliteraltoatemporaryStringobject,callsthemethod,thendiscardsthetemporaryStringobject.Youcanalsousethelengthpropertywithastringliteral.YoushouldusestringliteralsunlessyouspecificallyneedtouseaStringobject,becauseStringobjectscanhavecounterintuitivebehavior.Forexample:constfirstStri...
The String[1] object is a wrapper around the string primitive data type.
const foo = new String(foo); console.log(foo); typeof foo;You can call any of the methods of the String object on a string literal value—JavaScript automatically converts the string literal to a temporary String object, calls the method, then discards the temporary String object. You can also use the length property with a string literal.
You should use string literals unless you specifically need to use a String object, because String objects can have counterintuitive behavior. For example:
const firstString = 2 + 2; const secondString = new String(2 + 2); eval(firstString); eval(secondString);A String object has one property, length, that indicates the number of UTF-16 code units in the string. For example, the following code assigns helloLength the value 13, because "Hello, World!" has 13 characters, each represented by one UTF-16 code unit. You can access each code unit using...