Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is a string in JavaScript

Understanding Strings

Imagine you are writing a letter to your friend. You'd use words, sentences, and paragraphs to express your thoughts. In JavaScript, a similar concept exists but instead of sentences and paragraphs, we have what's called a string. A string is just a sequence of characters. It can be anything: a single character, a word, a sentence, or even an entire novel.

Let's say, for instance, we want to write "Hello, World!" in JavaScript. We would do this:

let greeting = "Hello, World!";

Here, greeting is a variable, and "Hello, World!" is a string.

Creating Strings

There are three ways to create strings in JavaScript: using single quotes (' '), double quotes (" "), or backticks (). The following are all valid strings:

let singleQuoteString = 'Single quote string';
let doubleQuoteString = "Double quote string";
let backtickString = `Backtick string`;

All three methods are practically the same, but backticks (`) allow us to embed variables and expressions, a feature we'll talk about shortly.

String Length

Understanding the length of a string is as simple as counting the number of characters in a sentence. In JavaScript, you can find out how many characters are in a string by using the .length property. Here's how:

let str = "Hello, World!";
console.log(str.length); // Outputs: 13

This tells us that the string "Hello, World!" has 13 characters, including the spaces and punctuation.

Accessing Characters in a String

Just like how you would pick out a certain word in a sentence, you can also pick out specific characters in a string. We do this by using brackets [].

let str = "Hello, World!";
console.log(str[0]); // Outputs: H

In the example above, str[0] gives us the first character of the string. In programming, we always start counting from zero, not one. That's why str[0] gives us the first character.

String Concatenation

Concatenation is a big word for a simple concept: joining things together. In the case of strings, it's about joining two or more strings together. We can do this using the + operator.

let greeting = "Hello";
let name = "John";
let message = greeting + ", " + name + "!";
console.log(message); // Outputs: Hello, John!

Template Literals

Earlier we mentioned that backticks (`) allow us to embed variables and expressions. This feature is known as a template literal. Template literals make string concatenation more intuitive.

let greeting = "Hello";
let name = "John";
let message = `${greeting}, ${name}!`;
console.log(message); // Outputs: Hello, John!

Common String Methods

Just like how a book has a table of contents, JavaScript provides a way to manipulate strings with built-in methods. Here are a few common ones:

  • toUpperCase(): converts all the string characters to upper case
  • toLowerCase(): converts all the string characters to lower case
  • trim(): removes spaces from the start and end of the string
  • includes(): checks if the string contains a certain character or substring
let str = " Hello, World! ";
console.log(str.toUpperCase()); // Outputs: " HELLO, WORLD! "
console.log(str.toLowerCase()); // Outputs: " hello, world! "
console.log(str.trim()); // Outputs: "Hello, World!"
console.log(str.includes("World")); // Outputs: true

Conclusion

Imagine a world without words, sentences, and books. It would be quite challenging to communicate, wouldn't it? That's how important strings are in JavaScript. Think of strings as the words and sentences of the programming world. They are a fundamental part of almost every JavaScript program, enabling us to represent and manipulate text data. So next time you're writing a love letter or a secret message, remember you're doing something very similar to what we do in programming when we work with strings.