Question about indexation Javascript

Hello Can I index 2 or more letter of a code in the same time ?
exemple

const lastName = "Lovelace";
console.log(lastName[0,4,5]);

You cannot put a list in like that. You would need to reference each one individually. There are other methods for string manipulation that might sometimes be what you’re looking for, depending on the specific goal.

I want using the first and the 3 last letters to make a new word. i need help please.

you just need to select each letter from the word on its own and then join them together, like you can do "a" + "b" to get "ab"

You can reference individual letters by index. You can also look at substring(), to see if that’s what you need:

Thank you

const lastName = "Lovelace";
console.log(lastName[0,4,5]);

You still can’t do that.

I am afraid you can’t access the characters in a string simultaneously due to the Javascript memory allocation management. You may slice the characters from the string and concatenate them to form a new one.

For example: console.log(lastName.slice(0,1) + lastName.slice(5));

Can’t I had [0,4,5] in my lastName to make a array , and this new array ,and I will find ["Love",0,4,5]. ?

No, JavaScript doesn’t allow you to do that (I don’t think any language does). You can access one character at an index.

The [] in this context is not an array. It uses the same square bracket characters, so I understand why you’re thinking it’s the same thing, but it isn’t the same thing. There are a limited amount of characters available on a normal keyboard, so some get reused for different purposes.

So you would do

console.log(lastName[0], lastName[4], lastName[5]);

“Console log the characters in the string assigned to the variable lastName at indices 0, 4 and 5”

1 Like