Day 5, #100DaysOfCode - Strings and String Manipulation - Jan 24, 2022
Photo by Drew Beamer on Unsplash

Day 5, #100DaysOfCode - Strings and String Manipulation - Jan 24, 2022

Today, I was able to take about nine lessons from @freecodecamp's data structure and algorithm curriculum.

The lessons spanned the following

  • Using escape sequences in strings
  • Concatenating strings with the "plus and plus equals" operators.
  • Constructing and appending strings with variables.
  • Finding the length of a string and using bracket notation to find specific characters of a string.
  • String immutability.

I've decided to make the posts shorter for easy readability, quite condensed.

Escape sequences in strings.

My previous posts show that escape sequences introduce characters/features that aren't allowed by default in Javascript strings. For example, escape sequences are used to introduce double quotes within a string as against the default nature of it being used as a string indicator. It can also introduce tabs, line breaks, carriage returns, word boundaries, and more. Essentially, it allows for a miniature form of text formatting.

The following list highlights the syntax of the various escape sequences:

  • Tab - \t
  • Newline - \n
  • Backslash - \\
  • Single quote - \'
  • Double quote - \"

Say we want to introduce a tab in a string of text

We have:

let myStr = "A tab in a \t text "; //this spaces the text in the same manner as the tab key like so

A tab in a      text.        

Every other sequence can also be used in the same manner described above.

Concatenating strings

To concatenate means to chain or link together in a series, and so to concatenate strings would mean to join or link multiple strings of characters in a series. This is usually done with the aid of the plus(+) operator as seen below;


var myStr = "This can be joined " + "with this to form a new string"; //Read on the browser console as "This can be joined with this to form a new string."
         

Notice that space is intentionally added at the end of the first string. This is because the operator literally joins both strings together, and so the space is added for the sake of readability in the console.

Constructing and appending strings with variables.

Strings can also be Concatenated using the "plus equals(+=) operator." In this case, a variable holding a string value is appended with another string using this operator like so:


var myStr = "The first string ";

myStr += "and the second string";

console.log(myStr); //on the browser console, this reads as: "The first string and the second string"
        

Notice that a space character was added at the end of the first string to enable readability when displayed on the console.

The last feature I learned with concatenating in Javascript is concatenating using variables like so:


let myStr = "The first variable " 

let mySecondStr = "and the second variable";

myStr += mySecondStr; // this reads as "The first variable and the second variable"
        

Also, we can have:

let newStr = "The new string is " + myStr ; //this concatenates the variable and the string.         

Finding the length of a string and using bracket notation to find specific characters of a string.

Moving on, I learned about finding the length of strings with the aid of the (.length) property in Javascript.

And so to find the length of my name, "David", we would have:


let myName = "David"

let nameLength = myName.length;

console.log(nameLength); //this reads as 5 on the browser console.
        

Furthermore, bracket notation can be used to find characters in a specific Index position within a string like so:


//to find the third character in "David

let myName = "David" ;

let thirdCharacter = myName[2];

console.log(thirdCharacter); //this reads as 'v' on the browser console.
        

The first character would be index (0), the second (1), and as seen, the third is (3). It's worth noting that, like most programming languages, Javascript also starts counting from zero(0), hence indicating '2' in the code block above.

String Immutability in Js

Today's last lesson is about Immutability I. E, the non-changing nature of strings in Javascript, which means that strings once declared can't be modified in any way.

For example, the following code block would throw an error in the browser console.

let myStr = "Boy";

myStr[0] = "J" ; //error. This wouldn't work as strings are immutable.
        

The only way to change strings is to reassign to a new string like so:


let myStr = "Boy" 

myStr ="Joy"; //the variable myStr is now read as "Joy" in the browser console.
        

Good! And so so that wraps up what I learned today, and I hope you were also able to follow along with me. Thanks for reading up to this point, and I hope to see you tomorrow. Please share your thoughts in the comments, and you share your reaction by smashing the thumbs-up button.

Cheers!

Picture credit: Photo by Drew Beamer on Unsplash

To view or add a comment, sign in

More articles by David Ogunniran

Others also viewed

Explore content categories