JavaScript Strings

What is String in JavaScript

A string is a series or combination of letters, integers, special characters, and mathematical values. Strings are produced by enclosing the string literal (i.e. string characters) between either single or double quotations (') ("). Strings in JavaScript are used to store and manipulate text. Within quotations, it can have zero or more characters.

Nevertheless, you may still use single or double quotes within single or double quotes by escaping the quotations with the backslash symbol (), like in the following example:

The sequences " and " that we used in the example above are known as escape sequences, whereas the backslash () is referred to as an escape character.



JavaScript Escape Sequences

Escape sequences are also useful when you need to use characters that cannot be typed on a keyboard. Here are some more frequently used escape sequences.

  1. The newline character replaces \n.
  2. The tab character replaces \t.
  3. The carriage-return character replaces \r.
  4. The backspace letter replaces \b.
  5. \\ is substituted with a single backslash (\).




In JavaScript, strings are a sequence of characters enclosed in single ('') or double ("") quotes. They are used to represent and manipulate textual data. Strings can contain letters, numbers, symbols, and whitespace. JavaScript provides various built-in methods and properties specifically designed for working with strings, such as extracting substrings, searching for patterns, modifying case, concatenating strings, and more. Strings play a crucial role in web development, powering tasks like input validation, data manipulation, and generating dynamic content.

String concatenation is the process of combining two or more strings to form a single string. In JavaScript, you can concatenate strings using the + operator or the concat() method. With the + operator, you simply use the operator between the strings you want to concatenate. For example, var fullName = firstName + " " + lastName; combines the firstName and lastName variables along with a space in between. Alternatively, the concat() method can be used, where you call it on one string and pass the other string(s) as arguments. For example, var greeting = "Hello, ".concat(name); appends the name variable to the existing string.

To find the length of a string in JavaScript, you can use the length property. The length property returns the number of characters in a string, including whitespace and special characters. For example, var message = "Hello, world!"; followed by console.log(message.length); will output 13, indicating that the string has a length of 13 characters. It's important to note that the length property does not start from 1, but rather from 0. Therefore, the index of the last character in the string will be length - 1. The length property is useful for validating the length of input fields, iterating over characters, and performing other string-related operations.