Thursday, February 5, 2026

Full Stack Development - Class 7- Strings Continued

 JavaScript String charAt()

let text = "HELLO WORLD";
let char = text.charAt(0);

JavaScript String charCodeAt()

let text = "HELLO WORLD";
let char = text.charCodeAt(0);

JavaScript codePointAt()

let text = "HELLO WORLD";
let code = text.codePointAt(0);

JavaScript String at()

const name = "W3Schools";
let letter = name.at(2);
Get the third letter of name
const name = "W3Schools";
let letter = name[2];
Immutable
let text = "HELLO WORLD";
text[0] = "A";    // Gives no error, but does not work

JavaScript String concat()

let text1 = "Hello";
let text2 = "World";
let text3 = text1.concat(" ", text2);

Note

All string methods return a new string. They don't modify the original string.

Formally said:

Strings are immutable: Strings cannot be changed, only replaced.

Extracting String Parts

There are 3 methods for extracting a part of a string:

  • slice(startend)
  • substring(startend)
  • substr(startlength)
let text = "Apple, Banana, Kiwi";
let part = text.slice(713);

let text = "Apple, Banana, Kiwi";
let part = text.slice(7);

let text = "Apple, Banana, Kiwi";
let part = text.slice(-12);

let text = "Apple, Banana, Kiwi";
let part = text.slice(-12, -6);

JavaScript String substring()

let str = "Apple, Banana, Kiwi";
let part = str.substring(713);

No comments:

Post a Comment