logoJourney BlogAffiliate links are used in this blog to earn income
WrappedUsername

WrappedUsername

Oct 28, 2022

Basic JavaScript Notes 2

Basic JavaScript Notes 2

Understanding String Immutability

In JavaScript, string values are immutable, which means that they cannot be altered once created.

For example, the following code:

let myString = "Bob"; myString[0] = "J";

cannot change the value of myString to Job, because the contents of myString cannot be altered. Note that this does not mean that myString cannot be changed, just that the individual characters of a string literal cannot be changed. The only way to change myString would be to assign it with a new string, like this:

let myStr = "Bob"; myStr = "Job";

Using bracket notation, finding characters in a string.

Bracket notation is a way to get a character at a specific index within a string. Most modern programming languages, like JavaScript, don't start counting at 1 like humans do. They start at 0. This is referred to as Zero-based indexing.

For example, the character at index 0 in the word Charles is C. So if const firstName = "Charles", you can get the value of the first letter of the string by using firstName[0]

for example:

const firstName = "Charles"; const firstLetter = firstName[0];

firstLetter would have a value of the string C.

Nth - Using bracket notation to get the Nth character in a string.

Nth is a naming convention that describes the position of a character in a string. Nth can represent any position of a character in a string.

For example:

const firstName = "Ada"; const secondLetterOfFirstName = firstName[1];

secondLetterOfFirstName would have a value of the string d.

In order to get the last letter of a string, you can subtract one from the string's length.

For example:

const firstName = "Ada"; const lastLetter = firstName[firstName.length - 1];

lastLetter would have a value of the string a.

Use the same principle to retrieve the last character in a string to retrieve the Nth-to-last character.

For example:

const firstName = "Augusta"; const thirdToLastLetter = firstName[firstName.length - 3];

thirdToLastLetter would have a value of the string s.

In Conclusion

  • Are you currently looking for a web3 blockchain job or other dev jobs? Check this out! Developer Jobs Here!
WrappedUsername

WrappedUsername

I am a self taught Solidity Smart Contract Auditor. Creator of this blog site.

Leave a Reply

Related Posts

background image

🎉 Thank you! 🎉

Our sponsor Foam Chunk Smudge NFT, an abstract art project, has created a token pass utility for this NFT. The Foam Chunk Smudge NFT token HODLer community on Discord has access to private channels with special vip only announcements, and much more! Check them out!

Our Sponsor

Categories

logoJourney Blog

© 2023 Journey Blog. All rights reserved.