Javascript String Reference and Code Samples

Concat()

We use the concat method to join 2 or more strings.

Explanation:

In this code we are declaring the variable for text1 as "Chicken".

Then we declare a variable for text2 as "Nuggets!"

When the code is executed, the words will combine giving you the final output of:

Chicken Nuggets!

<script>
    let text1 = "Chicken";
    let text2 = "Nuggets!";
    let text3 = text1.concat(" ",text2);
    document.getElementById("demo").innerHTML = text3;
</script>

Repeat()

We use the repeat method to repeat a string as many times as we want.

Explanation:

In this code we are declaring the variable for text as "Tater Nuts!".

Then we use 'let result' to define how many times we want the text to repeat.

When this code is executed, the text will print out 4 times in a row, without spaces in beween

'Tater Nuts!Tater Nuts!Tater Nuts!Tater Nuts!'

Adding .replace(/(?<=Tater Nuts!)(?=\w)/g, ' ') allows the code to print with spaces between.

'Tater Nuts! Tater Nuts! Tater Nuts! Tater Nuts!'

<script>
    let text = "Tater Nuts!" ;
    let result = text.repeat(4).replace(/(?<=Tater Nuts!)(?=\w)/g, ' ');
    document.getElementById("demo").innerHTML = text3;
</script>

Split()

We use the split method to break apart a string into a series of substrings.

Explanation:

In this code we are declaring the variable for text as 'a,b,c,d,e,f'.

Then we use 'const myArray' to specify how we want the string split, in this case a comma (',')

When this code is executed, the text will be split, and the output will be an array of substrings, like this

['a', 'b', 'c', 'd', 'e', 'f']

<script>
    let text = 'a,b,c,d,e,f';
    const myArray = text.split(",");
    document.getElementById("demo").innerHTML = myArray
</script>

Replace()

We use the replace method to replace a portion of a substring with something else.

Explanation:

In this code we are declaring the variable for text as 'I love my kids more than anything!'.

Then we use 'text.replaceAll' to specify what we want replaced, and with what value it's being replaced with.

When this code is executed, the text will replace 'anything' with 'this entire universe!' giving you the output of:

'I love my kids more than this entire universe!'

<script>
    let text = 'I love my kids more than anything!';
    text = text.replaceAll('anything', 'this entire universe!');
</script>

toUpperCase()

We use the toUpperCase() method to convert a string to all uppercase letters.

Explanation:

In this code we are declaring the variable for text by retrieving the content of an element with the ID "para".

Then, we apply the toUpperCase() method to convert all the letters of the string to uppercase.

When this code is executed, the text within the element will be converted to uppercase, giving you the output of:

'HELLO WORLD!'

<script>
    let text = document.getElementById("para").innerHTML;
    text = text.toUpperCase();
    document.getElementById("para").innerHTML = text;
</script>

toLowerCase()

We use the toLowerCase() method to convert a string to all lowercase letters.

Explanation:

In this code we are declaring the variable for text by retrieving the content of an element with the ID "para".

Then, we apply the toLowerCase() method to convert all the letters of the string to lowercase.

When this code is executed, the text within the element will be converted to lowercase, giving you the output of:

'hello world!'

<script>
    let text = document.getElementById("para1").innerHTML; {
    text = text.toLowerCase();
    document.getElementById("para1").innerHTML = text;
<script> }