JS Basic Datatypes - String

Download (.odt) Download (Markdown)

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

let string = "Hello world!";
let string2 = 'Hello world!';
let string3 = `Hello world!`;

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 string = ""Hello world!" - John Doe";
// this throws error because it's interpreted as an empty string ("") then something that doesn't fit any datatype and another string: " - John Doe"

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

let string = "\"Hello world!\" - John Doe"; // '"Hello world!" - John Doe'

It works the same way with apostrophes and backticks.

Add line break (\n)

[hook]
let string = "\"Hello world!\"\nJohn Doe";

Concatenation

[hook]

With + operator:

let name = "Fossery";
let string = "Hello " + name + "!"; // "Hello Fossery!"

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

let name = "Fossery";
let string = `Hello ${name}!`; // "Hello Fossery!"

With concat() method:

let name = "Fossery";
let string = "Hello ".concat(name, "!"); // "Hello Fossery!"

Get length of string (counting from 1)

[hook]
let string = "Hello world!";
let lengthOfString = string.length; // 12

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

[hook]

Treat it like an array:

let string = "Hello world!";
let firstChar = string[0]; // "H"
let hundredthChar = string[99]; // undefined

Use charAt() method:

let string = "Hello world!";
let firstChar = string.charAt(0); // "H"
let hundredthChar = string.charAt(99); // ""

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

let string = "Hello world!";
let firstChar = string.at(0); // "H"
let lastChar = string.at(-1); // "!"
let hundredthChar = string.at(99); // ""

Get all characters from start index to end index

[hook]

Use subtring() method:

let string = "Hello world!";
let hello = string.substring(0, 5); // "Hello" (0 is inclusive, 5 is exclusive)
let world = string.substring(6); // "world!" (if end index is not provided it matches until the last character of the string)

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

let string = "Hello world!";
let hello = string.slice(0, 5); // "Hello" (0 is inclusive, 5 is exclusive)
let world = string.slice(-6, -2); // "world"

Check if string includes another string

[hook]
let string = "Hello world!";
let string2 = "Hello";
let string3 = "hello";
let includesString2 = string.includes(string2); // true
let includesString3 = string.includes(string3); // false

Check if string starts with another string

let string = "Hello world!";
let string2 = "Hello";
let string3 = "world!";
let startsWithString2 = string.startsWith(string2); // true
let startsWithString3 = string.startsWith(string3); // false

Check if string ends with another string

let string = "Hello world!";
let string2 = "Hello";
let string3 = "world!";
let endsWithString2 = string.endsWith(string2); // false
let endsWithString3 = string.endsWith(string3); // true

Get starting index of first occurrence of a search term

[hook]
let string = "Hello world! Nice to meet you, world!";
let searchTerm = "world";
let whereIsFirstWorld = string.indexOf(searchTerm); // 6

Get starting index of last occurrence of a search term

let string = "Hello world! Nice to meet you, world!";
let searchTerm = "world";
let whereIsLastWorld = string.lastIndexOf(searchTerm); // 31

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

[hook]

Replace only the first occurrence using replace() method:

let string = "Hello world! Nice to meet you, world!";
let newString = string.replace("world", "Fossery"); // "Hello Fossery! Nice to meet you, world!"

Replace all occurrences using replaceAll() method:

let string = "Hello world! Nice to meet you, world!";
let newString = string.replaceAll("world", "Fossery"); // "Hello Fossery! Nice to meet you, Fossery!"

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

[hook]
let string = "Hello world! Nice to meet you, world!";
let words = string.split(" "); // ["Hello", "world!", "Nice", "to", "meet", "you,", "world!"]

For array operations, check out the Arrays cheatsheet.

Create uppercase version of a string

[hook]
let string = "Hello world! Nice to meet you, world!";
let uppercaseString = string.toUpperCase(); // "HELLO WORLD! NICE TO MEET YOU, WORLD!"

Create lowercase version of a string

let string = "Hello world! Nice to meet you, world!";
let lowercaseString = string.toLowerCase(); // "hello world! nice to meet you, world!"

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

let string = " \n Hello world! Nice to meet you, world! \n ";
let trimmedString = string.trim(); // "Hello world! Nice to meet you, world!"