DEV Community


Posted on

JS String Methods #4

In the name of Allah the most merciful and the most gracious
Yesterday we studied charAt method and today we study about charCodeAt(index) which we pass index inside curly braces.


This method returns or gives us the Unicode of a character.

let name='kamal';
//result -> 97
Enter fullscreen mode Exit fullscreen mode

As we see the result is 97 which implies the Unicode of 'a' is 97.

substring(startIndex,endIndex) and substr(startIndex,length)

substring requires two indexes which are the start index and the end index.

Difference between slice and substring is that the slice method accepts negative indexing but the substring method doesn't accept negative indexing.

let name='java';
//result -> jav
// result -> nothing
Enter fullscreen mode Exit fullscreen mode

Nothing means that substring does not accept negative indexing.
substr() is similar to slice().
The difference is that the second parameter specifies the length of the extracted part.

let fruits='apple banana orange pomegranate';
let citrus=fruits.substring(13,19);
//result -> orange
let vitaminCFruit=fruits.substr(20,11);
//result -> pomegranate
Enter fullscreen mode Exit fullscreen mode

Discussion (0)