Learning to Code: Day 40— Basic JavaScript Part 5
By: Hugh Burgess
Tags: Learning, Coding, JavaScript, Immutability, Arrays

Evening all! I hope this finds you well and ready to get stuck into class again, let’s pick up where we left off with FreeCodeCamp.

—

Find the Length of a String

Cause why not ey.

Simply type .length after the string to see it’s length:

var myStr = “This is a string.”.length;

Note: Write .length immediately after the string literal or string variable.

Use Bracket Notation to Identify the First Letter of a String

Bracket Notation is one way to identify a character at a particular index (a point in the data) within a string. So you could also say [1] or [200] or whatever to identify wherever you choose to identify the Nth number of a string.

Remember how coding counts from 0 onwards, not 1. So the first letter in a word is the 0th letter. This is called zero-based indexing.

Let’s find out the first letter of the word “Charles”, by defining it as the variable “firstName” first, then find the first letter by defining the variable “firstLetter” using the Bracket Notation 0 for the variable “firstName”:

var firstName = “Charles”; var firstLetter = firstName[0];

This will return the answer “C”.

Understanding String Immutability

One problem is that Strings cannot be amended (they are immutable) after they have been created. However, a string can be assigned a completely new value instead, this is a way to work around that immutability instead of trying to assign edits to the string. For example:

var myStr = “Hello”; myStr[0] = “J”;

-> This will not change the string to “Jello”.

var myStr = “Hello”; myStr = “Jello”;

-> This will change the string to “Jello”.

Find the Last Letter of a String

In the event that you want to find the last letter of a string, use this formula:

Within the Bracket Notation, minus 1 from the .length of the variable:

var myStr = “Charles”; var lastLetter = myStr[myStr.length -1];

The answer will be “s”.

Note: Using a higher number will index letters in the Nth-to-last position. For example, the third last letter of “Charles” (the letter l) would be [myStr.length -3]. This means the code is counting back 3 places from the end point of the string. Understand me?

Storing Multiple Values in One Variable with JavaScript Arrays

It’s possible to assign multiple values to one variable by separating each one with a comma, all inside square brackets. This is called an array declaration. These can be strings, numbers…

Here’s an example:

var sandwich = [“cheese”, “ham”, “salad”];

Nesting Arrays

Remember what nesting was? That’s putting one thing inside another, and here we’re able to ‘nest’ an array inside another, like so:

[[“eggs”, “ham”], [“cheese”, “salad”]]

Note: This is also called a multi-dimensional array.

Access Array Data with Indexes

FreeCodeCamp likes to teach one one thing followed by another, and then mix them together. We have learn’t how to index with Bracket Notation, and how to create an Array, now we’re going to use a Bracket Notation on the variable myArray with three nested values, and index that data within myArray, for example:

var myArray = [10, 40, 100]; myArray[0];

This gives 10.

var myData = Array[1];

This gives 40.

Remember: To think about the zero-based indexing structure.

Modifying Array Data with Indexes

It’s worth noting here that entries of arrays are mutable, unlike strings which are immutable, and can be freely edited after creation. Let’s hark back to this string example earlier which would not work:

var myStr = “Hello”; myStr[0] = “J”;

We can write virtually the same thing in the form of an array and it will work:

var myArray = [10, 20, 30]; myArray[0] = 15;

This will mean that the data inside myArray is now [15, 20, 30].

—

Aaaaand let’s quit whilst we’re ahead.

Phew! My head was starting to spin a little there. But I really like the style of JavaScript, it’s very regimented and logical. See you next time!