What Is a String in JavaScript? Types, Uses, and Examples

How-to basics · Updated November 22, 2026

A string in JavaScript is a standard primitive data type used to represent text: for example, 'hello', '42', or 'The page is ready.'. Strings are written inside quotation marks or backticks, and JavaScript provides operations for joining, searching, and transforming them. The phrase “a string JavaScript” is not the usual technical wording; people generally mean “a string in JavaScript.”

What the term means

In JavaScript terminology, string is the name of a built-in language type, not slang or an informal label. A string value is a sequence of text represented using UTF-16 code units. You can confirm the type with the typeof operator: typeof 'hello' evaluates to 'string'. A string might hold a person’s name, a sentence, a file path, or digits that should be treated as text.

The word comes from the ordinary English idea of a string as a length of cord or thread: in computing, characters are conceptually linked into a sequence. That metaphor is older and broader than JavaScript; there is no special JavaScript-specific origin for the word. The language uses the established programming term for textual data. In a sentence, someone might say, “The function returns a string containing the user’s display name.”

A string is not the same thing as a number that happens to look like text. '42' is a string, while 42 is a number. Their appearance may be similar, but JavaScript can perform different operations with them.

How to write a string

JavaScript has three string-literal forms: single quotes, double quotes, and backticks. Single- and double-quoted strings are both ordinary string literals. Choose either style consistently in a project; the choice does not make one value more textual or more valid than the other.

  • const city = 'Dublin'; creates a string with single quotes.
  • const greeting = "Hello!"; creates the same kind of value using double quotes.
  • const message = `Good morning`; uses a backtick, also called a template-literal delimiter.

Use a different quote style inside a string when that avoids escaping. For instance, 'She said, "hello."' contains double quotation marks as part of its text. If the text itself contains the delimiter, escape it with a backslash, as in 'It\'s ready'. The backslash is syntax for including the apostrophe; it is not part of the resulting value.

Backticks allow line breaks and substitutions called interpolation. A template such as `Hello, ${name}!` evaluates the expression inside ${...} and inserts its string representation into the result. By contrast, putting ${name} inside ordinary single or double quotes does not substitute the variable; those characters remain text.

Strings are primitive and immutable

JavaScript strings are primitive values. They are not arrays, even though both strings and arrays can be accessed by an index and have a length property. A string’s characters cannot be changed in place. For example, assigning to word[0] does not replace the first character. To make changed text, create a new string instead.

String methods also return results rather than modifying the original string. With const word = 'JavaScript', calling word.toUpperCase() produces 'JAVASCRIPT', while word remains 'JavaScript'. This matters when you expect a transformation to persist: assign the result to a variable or use it directly.

JavaScript also has a built-in String function and a String object constructor. Calling String(42) converts the number to the primitive string '42'. Using new String('hello'), however, creates a wrapper object rather than a primitive. Wrapper objects are rarely needed and can behave unexpectedly in comparisons and type checks, so ordinary string literals are usually the right choice.

Length, indexing, and Unicode

A string’s length reports the number of UTF-16 code units, not necessarily the number of visible characters. For common English letters, those counts match. Some symbols, including many emoji, use two code units. As a result, 'A'.length is 1, while '😀'.length is 2, even though the emoji appears as one symbol.

Similarly, bracket indexing and methods such as slice work with UTF-16 positions. They do not always correspond to what a reader thinks of as one character. Some visible symbols are made from multiple Unicode code points, such as an emoji combined with a skin-tone modifier or a letter plus a combining accent. If an application must count user-perceived characters precisely, it needs Unicode-aware handling, such as the Intl.Segmenter API in supported modern JavaScript environments, rather than assuming length counts them.

For ordinary text display, basic string operations are often sufficient. For text editing, character limits, or internationalized input, decide what “character” should mean in the application and test examples from the languages and symbols users may enter.

Common operations and methods

JavaScript supports joining strings with the + operator. For example, 'Hello, ' + 'Mina' produces 'Hello, Mina'. Template literals are often easier to read when combining text with variables. Strings can also be searched and transformed with built-in methods; these methods generally return a new value.

  • includes('cat') checks whether a substring appears and returns a Boolean. It is case-sensitive.
  • indexOf('a') returns the position of the first match, or -1 if there is no match.
  • slice(0, 4) returns a portion of a string, starting at index zero and ending before index four.
  • toLowerCase() and toUpperCase() return case-converted strings.
  • trim() returns a copy with whitespace removed from the beginning and end; it does not remove spaces inside the text.
  • replace('old', 'new') returns text with a match replaced. For repeated replacements, use an appropriate regular expression or replaceAll.
  • split(',') divides text at a separator and returns an array, not another string.

Here is a typical sequence: const raw = ' red,green,blue ';, then const colors = raw.trim().split(',');. The result in colors is the array ['red', 'green', 'blue']. This example shows why it is useful to distinguish a string from an array: splitting text changes the type of the result.

Converting values and inserting text

When a value must become text, use String(value) when you want an explicit conversion. For example, String(7) produces '7', and String(true) produces 'true'. Calling value.toString() can also convert many values, but it cannot be called on null or undefined because those values do not have that method. Choose a conversion that matches the values your code may receive.

The + operator can also convert values when one side is a string, which can surprise beginners. 'Total: ' + 7 produces the string 'Total: 7', while 7 + 2 produces the number 9. This is one reason to use template literals or an explicit conversion when the intention is to build text. It also helps prevent accidental mixing of text and arithmetic.

For example, with const name = 'Lee'; and const count = 3;, `${name} has ${count} messages.` produces 'Lee has 3 messages.'. The expressions are evaluated when the template literal runs. If a value is missing or has the wrong type, interpolation does not validate it for you; applications still need to check and format their data.

Common mistakes and safe handling

A frequent mistake is treating numeric-looking text as a number. The string '10' is not automatically suitable for arithmetic in every context. Convert and validate input deliberately, for example by using Number(input) and checking that the result is finite when appropriate. Conversely, keep an identifier such as a postal code as a string if leading zeroes or non-arithmetic formatting matter.

Another misconception is that text inserted into a string is automatically safe. Template literals only combine values; they do not escape content for HTML, SQL, a shell command, or another output format. If untrusted text is inserted into a web page, use a safe DOM API such as assigning plain text to an element’s textContent rather than building executable HTML with string concatenation. For database work, use the database library’s parameterized-query feature, not hand-built SQL strings. The right protection depends on the destination context.

Strings are also case-sensitive in many common comparisons and searches: 'Apple' === 'apple' is false, and 'Hello'.includes('hello') is false. If an application needs case-insensitive matching, define the intended normalization and locale behavior rather than assuming JavaScript ignores case.

Strings compared with numbers and arrays

A string represents text, a number represents a numeric value, and an array is an ordered collection that can hold multiple values. For example, 'red,green' is one string; ['red', 'green'] is an array with two string elements. Use a string for a piece of text or a value that should be treated as text, and an array when the program needs to work with separate items.

Conversion between these types is possible but not automatic in every operation. Number('12') converts a suitable numeric string to the number 12; String(12) converts it back to text. 'red,green'.split(',') returns an array, and ['red', 'green'].join(',') returns a string. Knowing the type at each step makes code easier to reason about and helps avoid bugs involving comparisons, calculations, and display.

Frequently Asked Questions

Is string a data type in JavaScript?
Yes. String is one of JavaScript’s primitive types. The expression typeof 'text' returns 'string'.

What is the difference between a string and a number?
A string stores text, even when that text contains digits, as in '42'. A number stores a numeric value, as in 42. Convert explicitly when you need arithmetic or textual output.

Do single quotes and double quotes behave differently?
For ordinary string literals, either delimiter creates a string. They mainly give you a convenient way to include the other quote mark without escaping it. Backticks additionally support interpolation and multiline template literals.

Are strings mutable in JavaScript?
No. A string cannot be changed in place. Methods such as replace and toUpperCase return new strings, leaving the original value unchanged.

Why can an emoji make a string’s length seem wrong?
The length property counts UTF-16 code units rather than visible symbols. Some emoji occupy two code units, and combined symbols can involve even more. Use Unicode-aware segmentation if your feature must count user-perceived characters.

What does “a string JavaScript” mean?
It is usually an imprecise way to ask about a string in JavaScript. The standard term is “JavaScript string” or “string in JavaScript,” meaning a text value of the language’s string type.

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.