Strings as Arrays

In JavaScript, we can treat text as an array of characters. Each character in the text has its own index, which allows us to access individual characters similarly to array elements.

Let's look at an example:

const text = "JavaScript";

In this example, the string text consists of ten characters. If we want to access the first character of the string, we use index 0:

const firstChar = text[0];
console.log(firstChar);  // Prints: 'J'

Similarly, we can access other characters:

const secondChar = text[1];
console.log(secondChar);  // Prints: 'a'

const thirdChar = text[2];
console.log(thirdChar);  // Prints: 'v'

Instructions

Store the first character of the firstName variable in a variable named firstChar.

Print the value of the firstChar variable.

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

9/10