Module 4 Participation | JS String References

Below are eight JavaScript string methods and properties for you to reference:

String LENGTH Property

Description: The length property returns the length of a string. In this situation the code sample below will return 13.

Code Sample:

let txt1 = "Hello, World!";
document.getElementById("lengthSample").innerText = txt1.length;

String indexOf Method

Description: The indexOf method returns the position of the first occurrence of a specified value in a string. In the sample below will give the index position of the value 'dreams' within the given striong, in this case, 4.

Code Sample:

let txt2 = "All dreams spin out from the same web.";
let position = txt2.indexOf("dreams");
document.getElementById("indexOfSample").innerText = position;

String slice Method

Description: The slice method extracts a section of a string and returns it as a new string. The sample below will take the string variable and slice the string at the given idex positons. In this situation, 'You Al'.

Code Sample:

let txt3 = "You Always Have A Choice.";
let sliced = txt3.slice(0, 6);
document.getElementById("sliceSample").innerText = sliced;

String toUpperCase Method

Description: The toUpperCase method converts a string to uppercase letters. In this case 'ALL THAT GLITTERS ISNT GOLD.'

Code Sample:

let txt4 = "All that glitters isnt gold.";
let upper = txt4.toUpperCase();
document.getElementById("toUpperCaseSample").innerText = upper;

String toLowerCase Method

Description: The toLowerCase method converts a string to lowercase letters. In this case 'free yourself.'

Code Sample:

let txt5 = "Free Yourself";
let lower = txt5.toLowerCase();
document.getElementById("toLowerCaseSample").innerText = lower;

String split Method

Description: The split method splits a string into an array of substrings based on a specified delimiter. For the string sample below will be converted into "My", "eyes", "are", "open", "now", "and", "can", "never", "be", "closed."

Code Sample:

let txt6 = "My eyes are open now and can never be closed.";
let parts = txt6.split(" ");
document.getElementById("splitSample").innerText = JSON.stringify(parts);

String trim Method

Description: The trim method removes whitespace from both ends of a string. The example below will be converted to only contain 'Alhamdulillah.'

Code Sample:

let txt7 = "   Alhamdulillah.   ";
let trimming = txt7.trim();
document.getElementById("trimSample").innerText = trimmed;

String replace Method

Description: The replace method replaces a specified value with another value in a string. In our string below the word 'day' will be replaced with 'time.' While the replace method on its own will only replace the first instance of your chosen substring by simply adding the global, g flag, you can change all occurences of the word. Ex (/day/g, "time").

Code Sample:

let txt8 = "Our day will come!";
let replaced = txt8.replace("day", "time");
document.getElementById("replaceSample").innerText = replaced;