5 things I learned using Javascript

Topics I may want to revisit someday

ยท

3 min read

5 things I learned using Javascript

Another update on my journey with Freecodecamp.org's JavaScript Algorithms and Data Structures Module.

In this article, I will share 5 things that I learned using Javascript that I will probably look back on and use in the future.


Converting String to Array

To convert a string to an array I used the spread operator "..."

let sVar = "Hello";
let aVar = [...sVar];

This code snippet would store "Hello" in aVar. It is equivalent to this

aVar =["H","e","l","l","o"];

Reversing an Array

To reverse an array I used the .reverse() method.

aVar.reverse();
console.log(aVar);

The above code would then output ["o","l","l","e","H"]


Converting an Array back to a String

My initial research pointed me to the method .toString(). This method, indeed, changes the array into a string but this method does not work with this example as it also appends a comma in between the letters.

/*This will result to "o,l,l,e,H"*/
console.log(aVar.toString());

Further research showed me that the method .join() is better.

let outString = aVar.join("");

.join() fuses the array together with the inputted parameter to make a string. The code above joins the array together with an empty String ("") in between the values. Thus the output of the code above is "olleH".

If we put "," as a parameter the code would have the same output as the toString() method.


Converting a String to an Array using Split()

Another way of converting a string to an array is by using the .split() method.

let sString = ("The quick brown fox jumped over the lazy dog");
let aVar = sString.split(" ");

.split(" ") splits the string into an array using " " (space) as a separator.

So the above method is the same as

let aVar = ["The", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog"];

The Difference between substring() and substr()

Although they both have the same idea, which is to get a part of a string and create a substring, the two methods function differently.


substring()

substring() accepts 2 parameters the first parameter is the starting index of the string to be cut and the second parameter is the ending index of the string to be cut but does not include the actual character on that index.

For example:

let sString = "Hello world!";
console.log(sString.substring(6,8));

So 6 will be the starting index which would be the letter "w".

Remember indexes start with 0

and 8 would be index 8 which is "r" but as mentioned before the output does not include index 8.

so the output would be

"wo"

Also note, that if the starting parameter is larger than the ending parameter it would act as if the smaller parameter is the starting point.

Example:


let sString = "Hello world!";
console.log(sString.substring(6,1));
console.log(sString.substring(1,6));

Both would output the word "hello".


###substr()

substr() also accepts 2 parameters. The first parameter is the starting index of the string to be cut and the second parameter is the number of characters from the starting index to be cut.

So to get the same output "wo" as before we use this code instead:


let sString = "Hello world!";
console.log(sString.substr(6,2));

So 6 will be the starting index which would be the letter "w". And we say 2 characters starting from 6 so that would be "w" and "o".


And that's it so far! Having a blast learning Javascript, but I'm still anxious as I have yet to put HTML, CSS, and Javascript together.


Thanks for reading this far ahead! If you want, you can contact me via Twitter here

I recently installed VS Code to try it out and I'm kinda lost. So if you have any advice or suggestions on how to use it to develop a landing page, kindly leave a comment below. I would really appreciate it!๐Ÿ˜…

ย