Strings are the one of the most used data types not only in JS but also in all programming language. Object are also very popular ( discuss object in other post).

1) Basic Information

String function and toString()

String can be converted from other types using String() function or by toString()

There exist no property like null.toString() as null and undefined also have no property ( See my post "Datatype in JS" for more detail)

String.fromCharCode( character_code , …)

String.fromCharCode(104, 101 , 108, 108, 111)  // "hello"check ASCII code for more information.

String through new keyword

String can be created using new keyword , but it is not recommended because string created through new keyword behaves more like as object instead of primitive type.

String.prototype.valueOf is use to retrieve the string value from string object.

2) String Concatenation

String concatenation can be done using “+” operator or with builtin String.concat() method.

3) String Interpolation

I prefer to  use `` method is it understands line break between `` as \n character, very helpful during writing dynamic HTML code in JS.

String.raw

String.raw is used to get the string with backslashes.

4) Comparing String Lexicographically

String.localeCompare()

This returns a negative value if the reference string is lexicographically (alphabetically) before the compared string , a positive value if it comes afterward , and 0 if the strings are equal.

Comparison is done on the basis of ASCII value of characters in the String.

Bonus

localeComparision clone

function strcmp(a,b) 
{
if(a === b)
return 0;
else if(a > b)
return 1;
else
return -1;
}
strcmp("hello" , "world") // -1
strcmp("hello" , "hello") // 0
strcmp("world" , "hello") // 1

Thanks a lot for your time , leave a clap if u got something new. I am planning for more parts for “String in JS”. Keep supporting me.

I am programmer and web developer who loves to teach