DEV Community

Cover image for LEVEL UP with JavaScript! LVL 6
DevCronin
DevCronin

Posted on • Updated on

LEVEL UP with JavaScript! LVL 6

In this blog series tutorial, I will be covering some of the basic JavaScript programming concepts.

This is geared toward beginners and anyone looking to refresh their knowledge.

See the Previous Level Here

Level 6 will cover:

  • Use Bracket Notation to Find the Nth Character in a String
  • Use Bracket Notation to Find the Last Character in a String
  • Use Bracket Notation to Find the Nth-to-Last Character in a String

Use Bracket Notation to Find the Nth Character in a String

If we want to find which character of a string is sitting at a particular value, we again use bracket notation with the number position we want to find [Nth].


let player = "Druid";

console.log(player[2]);

"u"

Enter fullscreen mode Exit fullscreen mode

Use Bracket Notation to Find the Last Character in a String

When we want to find the last character of a string, we can subtract one from the string's length.


let spell = "Fireball";

spell[spell.length - 1];

"l"

Enter fullscreen mode Exit fullscreen mode

Use Bracket Notation to Find the Nth-to-Last Character in a String

Just as we did before to find the Nth and the last character in a string, we can combine the two to find the last to Nth character.


let enemy = "Goblin Mage";

let secondToLastLetter = enemy[enemy.length - 2];

console.log(secondToLastLetter);

"g"

Enter fullscreen mode Exit fullscreen mode

Thank you for reading my blog! This is the sixth of my series on JavaScript so if you would like to read more, please follow!

See the Next Level Here

Support and Buy me a Coffee

Discussion (4)

Collapse
jonrandy profile image
Jon Randy • Edited

Huh? Not sure what you're talking about here - strings are not immutable in JS. Your console.log(name) will show Wizard in the console. The return value of console.log is undefined, but that is always the case with console.log - whatever you pass to it

Collapse
lukeshiru profile image
LUKESHIRU

Yes and no. Strings are immutable:

const example = "foo";
example[0] = "x"; // This does nothing
example === "foo"; // So this is true
Enter fullscreen mode Exit fullscreen mode

But as you mentioned, the example @devcronin gave with undefined, doesn't make any sense. console.log always returns undefined, so that has nothing to do with mutation. Also the example is not actually "mutating" name, is just assigning a new value to it.

Collapse
devcronin profile image
DevCronin Author

I am a Jr dev and believe this was just a mistake on my part. I do my best to ensure I keep my content accurate. Thank you both for replying and pointing out my error.

Collapse
ifierygod profile image
Goran Kortjie

Alt Text