JavaScript Strings

What are strings?

Strings are one of the basic data types in JavaScript. They are used to store and manipulate text, such as names, messages, commands, etc.

A string is a sequence of zero or more characters, enclosed by either single quotes ('), double quotes ("), or backticks (\`). For example:

Javascript
                        
let name = 'Alice'; // a string with single quotes
let greeting = "Hello, world!"; // a string with double quotes
let command = `echo ${name}`; // a string with backticks

You can use any of these quotes to create a string, as long as they match at the beginning and the end. You can also use quotes inside a string, as long as they are different from the ones surrounding the string. For example:

Javascript
                        
let quote = "She said 'Hello' to me"; // a string with double quotes and single quotes inside
let question = 'What does "JS" stand for?'; // a string with single quotes and double quotes inside

If you want to use the same quotes inside and outside a string, you need to use the backslash (\) character to escape them. For example:

Javascript
                        
let message = "She said \"Hello\" to me"; // a string with double quotes and escaped double quotes inside
let answer = 'It\'s JavaScript'; // a string with single quotes and escaped single quotes inside

The backslash character can also be used to escape other special characters, such as newline (\n), tab (\t), backspace (\b), etc. For example:

Javascript
                        
let poem = "Roses are red\nViolets are blue\nJavaScript is fun\nAnd so are you"; // a string with newline characters
let indent = "\tThis is indented"; // a string with a tab character

How to access and modify strings?

You can access the individual characters of a string by using the bracket notation ([]) and the index of the character. The index starts from 0 for the first character, 1 for the second, and so on. For example:

Javascript
                        
let word = 'JavaScript';
let first = word[0]; // 'J'
let last = word[9]; // 't'

You can also use the charAt() method to get the character at a specific index. For example:

Javascript
                        
let word = 'JavaScript';
let first = word.charAt(0); // 'J'
let last = word.charAt(9); // 't'

You can get the length of a string by using the length property. For example:

Javascript
                        
let word = 'JavaScript';
let len = word.length; // 10

You cannot modify the characters of a string by using the bracket notation or the charAt() method. This is because strings are immutable in JavaScript, which means they cannot be changed after they are created. For example:

Javascript
                        
let word = 'JavaScript';
word[0] = 'K'; // this will not work
word.charAt(0) = 'K'; // this will not work either

If you want to change a string, you need to create a new string with the modified content. You can use various methods and operators to create new strings from existing ones. For example:

Javascript
                        
let word = 'JavaScript';
let newWord = 'K' + word.slice(1); // 'KavaScript'
let anotherWord = word.replace('Java', 'Type'); // 'TypeScript'

What are string literals and string objects?

There are two ways to create strings in JavaScript: using string literals or using the String() constructor.

String literals are the most common way to create strings. They are simply the text enclosed by quotes, as we have seen before. For example:

Javascript
                        
let name = 'Alice'; // a string literal
let greeting = "Hello, world!"; // another string literal

String literals are primitive values, which means they are not objects and they have no methods or properties of their own. However, JavaScript allows you to access the methods and properties of the `String` object on string literals, by converting them to temporary string objects behind the scenes. For example:

Javascript
                        
let name = 'Alice'; // a string literal
let upperName = name.toUpperCase(); // 'ALICE'
// JavaScript converts name to a temporary string object and calls the toUpperCase() method on it

The String() constructor is a function that creates a new string object from a given value. You can use the new keyword to invoke the constructor, or you can omit it and use the constructor as a function. For example:

Javascript
                        
let name = new String('Alice'); // a string object with the new keyword
let greeting = String('Hello, world!'); // a string object without the new keyword

String objects are objects that have the methods and properties of the String prototype. They are different from string literals in some ways. For example:

  • String objects have a typeof value of "object", while string literals have a typeof value of "string".
  • String objects are compared by reference, while string literals are compared by value. This means that two string objects with the same content are not equal, while two string literals with the same content are equal.
  • String objects are mutable, while string literals are immutable. This means that you can add or modify properties on string objects, but not on string literals.

Here are some examples to illustrate these differences:

Javascript
                        
let name1 = 'Alice'; // a string literal
let name2 = 'Alice'; // another string literal
let name3 = new String('Alice'); // a string object
let name4 = new String('Alice'); // another string object

typeof name1; // "string"
typeof name3; // "object"

name1 == name2; // true
name3 == name4; // false

name1[0] = 'K'; // this will not work
name3[0] = 'K'; // this will work

name1.foo = 'bar'; // this will not work
name3.foo = 'bar'; // this will work

In general, it is recommended to use string literals instead of string objects, because they are simpler, faster, and less error-prone. String objects are rarely needed, unless you want to create your own custom methods or properties on them.