JavaScript String

In this tutorial, you will learn about JavaScript string with the help of examples.

JavaScript string is a primitive data type that is used to work with texts. For example,

const name = 'John';

Create JavaScript Strings

In JavaScript, strings are created by surrounding them with quotes. There are three ways you can use quotes.

  • Single quotes: 'Hello'
  • Double quotes: "Hello"
  • Backticks: `Hello`

For example,

//strings example
const name = 'Peter';
const name1 = "Jack";
const result = `The names are ${name} and ${name1}`;

Single quotes and double quotes are practically the same and you can use either of them.

Backticks are generally used when you need to include variables or expressions into a string. This is done by wrapping variables or expressions with ${variable or expression} as shown above.

You can also write a quote inside another quote. For example,

const name = 'My name is "Peter".';

However, the quote should not match the surrounding quotes. For example,

const name = 'My name is 'Peter'.'; // error

Access String Characters

You can access the characters in a string in two ways.

  • One way is to treat strings as an array. For example,
const a = 'hello';
console.log(a[1]); // "e"
  • Another way is to use the method charAt(). For example,
const a = 'hello';
console.log(a.charAt(1)); // "e"

JavaScript Strings are immutable

In JavaScript, strings are immutable. That means the characters of a string cannot be changed. For example,

let a = 'hello';
a[0] = 'H';
console.log(a); // "hello"

However, you can assign the variable name to a new string. For example,

let a = 'hello';
a = 'Hello';
console.log(a); // "Hello"

JavaScript is Case-Sensitive

JavaScript is case-sensitive. That means in JavaScript, the lowercase and uppercase letters are treated as different values. For example,

const a = 'a';
const b = 'A'
console.log(a === b); // false

In JavaScript, a and A are treated as different values.

JavaScript Multiline Strings

To use a multiline string, you can either use the + operator or the \ operator. For example,

// using the + operator
const message1 = 'This is a long message ' +
    'that spans across multiple lines' + 
    'in the code.'

// using the \ operator
const message2 = 'This is a long message \
that spans across multiple lines \
in the code.'

JavaScript String Length

To find the length of a string, you can use built-in length property. For example,

const a = 'hello';
console.log(a.length); // 5

JavaScript String Objects

You can also create strings using the new keyword. For example,

const a = 'hello';
const b = new String('hello');

console.log(a); // "hello"
console.log(b); // "hello"

console.log(typeof a); // "string"
console.log(typeof b); // "object"

Note: It is recommended to avoid using string objects. Using string objects slows down the program.

JavaScript String Methods

Here are the commonly used JavaScript String methods:

Method Description
charAt(index) returns the character at the specified index
concat() joins two or more strings
replace() replaces a string with another string
split() converts the string to an array of strings
substr(start, length) returns a part of a string
substring(start,end) returns a part of a string
slice(start, end) returns a part of a string
toLowerCase() returns the passed string in lower case
toUpperCase() returns the passed string in upper case
trim() removes whitespace from the strings
includes() searches for a string and returns a boolean value
search() searches for a string and returns a position of a match

Example: JavaScript String Methods

const text1 = 'hello';
const text2 = 'world';
const text3 = '     JavaScript    ';

// concatenating two strings
const result1 = text1.concat(' ', text2);
console.log(result1); // "hello world"

// converting the text to uppercase
const result2 = text1.toUpperCase();
console.log(result2); // HELLO

// removing whitespace from the string
const result3 = text3.trim();
console.log(result3); // JavaScript

// converting the string to an array
const result4 = text1.split();
console.log(result4); // ["hello"]

// slicing the string
const result5= text1.slice(1, 3);
console.log(result5); // "el"

JavaScript String() Function

The String() function is used to convert various data types to strings. For example,

const a = 225; // number
const b = true; // boolean

//converting to string
const result1 = String(a);
const result2 = String(b);

console.log(result1); // "225"
console.log(result2); // "true"

If you want to learn more about the string conversion, visit JavaScript Type Conversion.

Escape Character

You can use the backslash escape character \ to include special characters in a string. For example,

const name = 'My name is \'Peter\'.';


My name is 'Peter'.

In the above program, the same quote is included using \.

Here are other ways that you can use \:

Code Output
\" include double quote
\\ include backslash
\n new line
\r carriage return
\v vertical tab
\t horizontal tab
\b backspace
\f form feed
Did you find this article helpful?