Course

Length and Basic Properties

In this lesson, you'll learn about some common operations you can perform on strings in JavaScript, such as finding their length and accessing specific characters.

String Length

One thing we often want to know about strings is their length. You might think that calculating this requires complex operations, like looping through all the characters and counting them. Fortunately, it's much simpler than that!

To find the length of a string, you can use the property:

const name = 'John';

console.log(name.length); // 4

Accessing Characters in a String

Another common operation is accessing a character at a specific position in a string. This is also straightforward.

To get the first letter of a string:

console.log(name[0]); // J

To get the last letter of a string:

console.log(name[name.length - 1]); // n

Let's break down this line:

  • is equal to .
  • minus is .
  • is the last letter because string indices start from , not .

In the same way, you can access any character in the string:

console.log(name[2]); // h

In this lesson, we learned how to get the length of a string using the property and how to access specific characters within a string using bracket notation .

Now let's learn how we can change the case of a string!

Loading...

0 Comments

"Please login to view comments"

glass-bbok

Join the Conversation!

Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.

Upgrade your account
tick-guideNext Lesson

Change String Case