Working with Strings

Strings are one of the most common things you'll work with in JavaScript. They are used to store and handle text, like names, messages, or anything written with letters, numbers, or symbols. In this lesson, you’ll learn how to create strings, join them together, and use useful tools to work with them easily.

Creating strings with quotes/backticks

You can create a string in JavaScript by putting the text inside quotes. There are three types you can use:

  • Double quotes: "Hello"
  • Single quotes: 'Hello'
  • Backticks (template literals): ` Hello `

All three work the same way, but using backticks gives you some extra power (we’ll see that soon).

Here are a few examples:

javascript
1
2
3
          console.log("My name is Jeff");
console.log('Welcome to JSBites');
console.log(`JavaScript is fun`);
        

You can even use one type of quote inside another:

javascript
1
2
          console.log("She said: 'Hi!'");
console.log('He replied: "Hello!"');
        

But if you use the same type of quote inside the string, you’ll get an error. To fix that, you use the backslash \ to escape it:

javascript
1
2
          console.log("My name is \"Jeff\"");
console.log('It\'s a great day!');
        

There are also special escape characters like:

  • \n – adds a new line
  • \\ – adds a backslash

Example:

javascript
1
          console.log("First Name: Jeff\nLast Name: John");
        

Concatenation and template literals

Concatenation means combining two or more strings into one. You can do this using the + operator:

javascript
1
          console.log("Hello, " + "world!");
        

If you want a space between the strings, add it manually:

javascript
1
          console.log("Hello" + " " + "world!");
        

You can also join strings stored in variables:

javascript
1
2
3
          let firstName = "Jeff";
let lastName = "John";
console.log(firstName + " " + lastName);
        

Now let’s look at template literals using backticks. They make combining strings easier and more readable. You can insert variables directly inside the string using ${}:

javascript
1
2
          let name = "Jeff";
console.log(`Hello, my name is ${name}`);
        

This is much cleaner, especially when working with multiple values or creating longer strings.

length Property

Tells you how many characters are in the string (including spaces):

javascript
1
2
          let message = "Good Morning";
console.log(message.length); // Output: 12
        

String methods

JavaScript gives you built-in methods to work with strings. Here are some of the most useful ones for beginners:

toUpperCase() Method

Converts the string to all capital letters:

javascript
1
2
          let name = "jeff";
console.log(name.toUpperCase()); // Output: "JEFF"
        

slice(start, end) Method

Cuts a part of the string and returns it. You give it a starting index and (optionally) an ending index:

javascript
1
2
          let greeting = "Good Morning";
console.log(greeting.slice(0, 4)); // Output: "Good"
        

Remember: string indexes start from 0. So in the example above, "G" is at position 0, "o" at 1, and so on.