DEV Community

ahmadullah
ahmadullah

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.

charCodeAt(index)

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

let name='kamal';
console.log(name.charCodeAt(1));
//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';
console.log(name.slice(-1,-4);
//result -> jav
console.log(name.substring(-1,-3);
// 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);
console.log(citrus);
//result -> orange
let vitaminCFruit=fruits.substr(20,11);
console.log(vitamicCFruit);
//result -> pomegranate
Enter fullscreen mode Exit fullscreen mode

Top comments (0)