JS Basic Datatypes - String

Download (.odt) Download (Markdown)

A series of characters inside quotation marks, apostrophes or backticks.

Escape characters inside a string

[hook]

If you want to contain a character inside a string which indicates the start and end of string you have to escape that character to consider it as a regular character.

Let's fix it by adding backslashes before the inner quotation marks.

It works the same way with apostrophes and backticks.

Add line break (\n)

[hook]

Concatenation

[hook]

With + operator:

With expression inside string (only works with strings indicated with backtick!):

With concat() method:

Get length of string (counting from 1)

[hook]

Get a character at a specific index (counting from 0)

[hook]

Treat it like an array:

Use charAt() method:

Use at() method (also supports negative indexing):

Get all characters from start index to end index

[hook]

Use subtring() method:

Use slice() method (also supports negative indexing):

Check if string includes another string

[hook]

Check if string starts with another string

Check if string ends with another string

Get starting index of first occurrence of a search term

[hook]

Get starting index of last occurrence of a search term

Create modified version of a string by replacing a piece of text

[hook]

Replace only the first occurrence using replace() method:

Replace all occurrences using replaceAll() method:

Create a new array from a string splitted by a pattern (string or regex)

[hook]

For array operations, check out the Arrays cheatsheet.

Create uppercase version of a string

[hook]

Create lowercase version of a string

Create new string from original without whitespaces at the start and end