#100DaysOfVanillaJS: All About Strings, String Methods, & Template Literals 
Tags: javascript, webdev, beginners, programming
Published: 2020-04-10 10:12:48

---
title: #100DaysOfVanillaJS: All About Strings, String Methods, & Template Literals 
published: true
description: Let's dive deep into the world of strings, a fundamental building block of JavaScript
tags: #Javascript, #webdev, #beginners, #programming 
canonical_url: https://taeluralexis.com/2020/04/22/100daysofvanillajs-all-about-strings-string-methods-template-literals/
---
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/bklpym5jpgubzh0j2dke.gif)

Strings are an essential building block of any application. They are pieces of text encapsulated in either single- or double quotes. They can contain any letter, numbers, or special characters. It's important to understand how every character in a string has a positional value that can be accessed and also vital to know how you can use a variety of built-in methods (actions) to perform what you need to do. I'll show you how with examples. Ready to get started? Open up your JavaScript console and let's get to learning!

### Indexing 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/kcqqrfbqup7q7d6lmld0.png)
Always remember that every character in a string (including whitespace) has its own positional value starting at 0. The way I understood that was by counting the numbers of characters within a string with my finger & remembering to count from zero. It's called <strong>zero-indexing<strong>. It's important to know this because you can easily access specific characters in a string. 

Every string contains a <strong>length property</strong> that tells you how many characters are in a string. 

The length of a string is always one greater than the last index.

Strings are immutable meaning you cannot change the individual value.


### String Concatenation
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/zz5jmjio6usax5a928cb.png)
You can use the addition operator to "concatenate" or combine strings. If you wanted to include space to separate the words and make it more readable, add whitespace either within the string or add space between the strings with quotation marks.

If you were to try to use the subtraction operator with 2 strings, you will get a return value of NaN (or Not a Number). 

### So What Are String Methods?
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/cipod2d354js3b4si6y1.png)

Methods are built-in actions that can be performed on any string and there are quite a few of them available for you to use. You can replace characters within a string, slice, search within, trim, capitalize or lowercase characters, and split, just to name a few. 

You use dot notation to perform such actions as shown below. Be mindful that simply performing any methods on a string will not change the original string. In order to do that you would have to create a new variable where the value equals the string name 

I highly encourage you to play around with the different string methods to get comfortable with manipulating strings. It is often asked of you to do so interviews to be honest.

### String Arguments
Some methods let you pass in additional information inside of the parentheses to modify their behavior. Let me show you by example. I'll provide a solid example below.

### To Find indexOf(), Slice, Or Replace, Goku asks

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/9md0iqruszc640ik5nwu.png)

I wanted to point out 3 very important methods that I often to utilize in front-end interviews and on the job while working on more complex applications: .index(), .slice(), or .replace().

<strong>.()index</strong> method is where you can perform a search to see if a string contains specified characters. It will return the position of where the occurrence of the substring starts. It always returns a number. If it cannot be found then the answer will be -1. 

<strong>.slice(</strong> method accepts one or two arguments and allows you to "slice" or cut out a sub-string of the string. It does not alter or change the actual string because again, strings are immutable. You can pass in 2 arguments (where slice begins and ends in a string)

<strong>.replace()</strong> methods takes in 2 arguments: the first argument is where you specify what you want to replace within the string and the second argument lets you describe what you want to replace it with.

### Template Literals 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/t0yb7piqt0ahcxbtbnck.png)

Introduced in ES6, template literals allow you to interpolate strings which means you can evaluate and perform methods in strings. It is common in many other languages like Python, JavaScript is just catching up.

The code snippet in this section provide some practical examples to help you understand how it works. Be mindful that each template literal starts and ends with back ticks, not quotation marks like regular string and the expression you want to be evaluated within the string must start with a dollar sign and be encapsulated with brackets.

You can also use back ticks to create multiple lines which makes for even more readable code so this replaces the need to perform an escape sequence using the <em>/n</em> keyword. To format your code a bit better, perform the .trim() method outside of the string to rid yourself of whitespace.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/06iai31zgu88rgye505a.png)


### What Will We Learn Next?

Hope you've enjoyed learning about how to create and manipulate strings utilizing all of the different methods we can use at our disposal. Next we'll learn more about the differences between null and undefined. 

<ul>
  <li> Null & Undefined </li>
  <li> Loops, Objects, Arrays </li>
</ul>