# JS Strings - Properties and Methods

Here is a collection of useful properties and methods that are used on Javascript strings. This is not an  [exhaustive list](https://www.w3schools.com/jsref/jsref_obj_string.asp) , rather it is a list of some of the ones that I have found most useful in my development.

---

A string is defined in JavaScript using the `var` keyword, like so:
```
var name = "John Doe";
```

A string can be anything inside either double or single quotes: `"This is a string"`, but also `'so is this'`.



---

## Properties

**`length`**
One of the properties that strings have in JS is the `length` property. It simply returns the number of characters in that string. To illustrate, we will use the `name` string that we created earlier.

```
name.length;             //returns 8
```


---

## Methods
Strings have a vast number of methods. 

**`charAt()`**
This method returns the character that is at the specified index in the string. Note that JS uses zero-based counting, meaning that the first character is at the 0-index, the second character is at the 1-index, and so on.

```
name.charAt(0);             //returns "J"
name.charAt(1);             //returns "o"
```


**`charCodeAt()`**
This method is similar to `charAt()` in that it returns the character at an index. This method, however, returns the  [unicode number](https://unicodelookup.com/)  for the character in that spot. In this case, the letter "J" has the unicode number 74.

```
name.charCodeAt(0);       //returns 74
```


**`concat()`**
This method combines two or more strings. Note that it does not change any of the original strings; instead it returns a new string composed of the original strings.

```
var welcome = "Welcome ";
var greeting = ". Greetings."
welcome.concat(name);             //returns "Welcome John Doe"
welcome.concat(name, greeting);   //returns "Welcome John Doe. Greetings."
```


**`endsWith()`**
Checks to see if a string ends with the specified characters.  This method accepts multiple characters, as such:

```
name.endsWith('Doe');        //returns true
```


**`includes()`**
Checks to see if a string is included in another string. This method is case sensitive.

```
name.includes("John");      //returns true
```

**`indexOf()`**
Searches the string for the first occurrence of the specified value. If value is never found, it returns `-1`.

```
name.indexOf('o');        //returns 1
name.indexOf('w');        //returns -1
```


**`lastIndexOf()`**
Searches the string for the last occurrence of the specified value. If value is never found, it returns `-1`.

```
name.lastIndexOf('o');        //returns 6
name.lastIndexOf('w');        //returns -1
```


**`match()`**
Searches the string for the first match of a string using regular expressions. If the global flag is passed, it will return all matches of that string.

```
name.match(/o/);      //returns ["o", index: 1, input: "John Doe", groups: undefined]
name.match(/o/g);     //returns ["o", "o"]
```


**`search()`**
Searches the string for the first occurrence of a specified value, and returns the index of that value. 
`search()` is different from `indexOf()` in that it can search for both regular expressions and simple strings.

```
name.search(/o/);     //returns 1    
name.match("o");      //returns 1
name.match("w");      //returns -1
```



**`slice()`**
Extracts a segment from a larger string, returning the segment.

```
name.slice(0,4);     //returns "John"
```


**`split()`**
Splits a string into an array of substrings based on a string.

```
name.split(" ");      //returns ["John", "Doe"]
name.split("o");      //returns ["J", "hn D", "e"]
```

---

For more information and guides on Javascript, check out the  [Mozilla Developer Network](https://developer.mozilla.org/en-US/docs/Web/JavaScript)  or  [W3Schools](https://www.w3schools.com/jsref) .
