A string is a sequence of characters treated as a single piece of data.

It is a type of data that stores texts in which programmers use to store textual information. This data is obtained through a user’s inputs so they can use it to manage their programs.

A string is one of the data types in programming, and it is a part of fundamental programming principles. You will need to know this concept as it is essential for building software programs.

How it works

A string can contain all characters like alphabetic letters (A-Z), symbols (!@#&), numbers (123), empty spaces, and emojis (πŸ˜€βš½πŸ‰) as well.

For example, “Pizza,” “John Wick,” and “NP123456ES” are all strings. Numbers can also be a string when enclosing with quotation marks. “1234” is a string, but 1234 is an integer which is a different type of data.

Each programming language has different ways of declaring strings and how they implement in the program. What do they all have in common is they are usually surrounded by quotation marks.

If you do not cover a string with quotation marks on both sides, a programming language will see it as different data and may cause an error.

Example in JavaScript

Let take a look at some examples of a string using JavaScript programming language.

To create a string in JavaScript, you can declare it as primitive or literal strings:

const string = "This is a string."

console.log(string); // prints "This is a string."

You can create strings using single or double quotation marks:

const stringOne = 'This string has single quotation marks.'
const stringTwo = "This string has double quotation marks."

console.log(stringOne); // prints 'This string has single quotation marks.'
console.log(stringTwo); // prints 'This string has double quotation marks.'

If you want to insert quotation marks inside the strings, there are two ways to do this: Use the backslash ‘' before the quote mark to escape the character.

const string = "I got the \"best\" cake ever!";

console.log(string); // prints "I got the "best" cake ever!"

Another way is to use quotation marks inside the strings, but the quotation mark should not be the same as the surrounding string.

const stringOne = 'This is "correct" answer.';
const stringTwo =  "It's a nice car.";

console.log(stringOne); // prints 'This is the "correct" answer.'
console.log(stringTwo); // prints "It's a nice car."

As you can see from the example above, a double quote is inside the string covered by single quotes, and single quotes are inside the string covered by a double quote. Both approaches are valued strings in JavaScript.

Conclusion

Here is a summary of what you learn about string so far:

  • A string is a sequence of characters treated as a single piece of data.
  • A string can have alphabetic letters, symbols, numbers, space, and emojis.
  • Numbers can also be a string when enclosing with quotation marks. “1234” is a string, but 1234 is not a string.
  • If you see any characters enclosed with quotation marks, then it’s a string.
  • In JavaScript, you can create strings using single or double quotation marks.
  • To insert quotation marks inside the strings, use the backslash ‘' before the quote mark.
  • You can also use quotation marks inside the strings, but the quotation mark should not be the same as the surrounding string.

Get my free e-book to prepare for the technical interview or start to Learn Full-Stack JavaScript