JavaScript Strings

JavaScript strings are used to store and manipulate text. They are a sequence of characters, enclosed in quotes (either single or double). For example, "hello, world!" or 'this is a string'.

Strings are used to represent text in JavaScript and can be manipulated using various string methods and properties.

They can also be concatenated using the + operator. For example: "Hello, " + "world!" would return "Hello, world!".

String Length

In JavaScript, you can find the length of a string by using the built-in property called length.

For example:

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

In this example, the length property is used to find the number of characters in the string "Hello, World!", which is 13.

The value of the length property is stored in the variable len and then logged to the console.

Another way is to use .length property and wrap a string inside the [] brackets like:

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

In this case, the string is not assigned to any variable but directly its length property is used.

JavaScript Escape Sequences

In JavaScript, certain characters have special meaning, such as newline \n, tab \t, and double quote \" for example.

To use these characters as regular characters in a string, they must be escaped using the backslash \ character. This is known as an escape sequence.

Here are some common escape sequences in JavaScript:

  • \' - single quote
  • \" - double quote
  • \\ - backslash
  • \n - newline
  • \r - carriage return
  • \t - tab
  • \b - backspace
  • \f - form feed

For example, if you want to include a double quote inside a string that is enclosed in double quotes, you would need to escape it like this:

let str = "He said, \"Hello, World!\"";
console.log(str); 
// Output: He said, "Hello, World!"

Similarly, if you want to include a backslash inside a string, you would need to escape it like this:

let str = "This is a backslash: \\";
console.log(str); 
// Output: This is a backslash: \

It's important to note that when using a template literals ` the escape sequence is not necessary, like this:

let str = `He said, "Hello, World!"`;
console.log(str);

It's also important to note that JavaScript also supports the use of Unicode escape sequences, which allows you to include any Unicode character in a string using its 4-digit hexadecimal code point.

For example, to include the Euro symbol in a string, you can use the Unicode escape sequence \u20AC.

let str = "Price in Euro : \u20AC20"
console.log(str) // Output: Price in Euro : €20

JavaScript Strings as Objects

JavaScript strings are primitive data types, which means they are not objects and do not have properties and methods like objects do. However, JavaScript strings can be treated as objects when they are accessed using the new keyword and the String constructor.

When a string is accessed using the new keyword and the String constructor, it is converted to a String object. This allows you to use the properties and methods of the String object to manipulate the string.

For example:

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

In this example, the string "Hello, World!" is converted to a String object using the new keyword and the String constructor. The length property of the String object is then used to find the number of characters in the string.

It's important to note that, although it's possible to use JavaScript strings as objects, it's not a recommended practice. This is because it's more memory-intensive, and the performance is not the best.

It's better to use the methods and properties of the string directly and avoid using the new keyword and the String constructor.

Instead of using the new keyword and the String constructor, you can use the built-in string methods to manipulate strings in JavaScript.

These methods include:

  • .length
  • .toUpperCase()
  • .toLowerCase()
  • .indexOf()
  • .lastIndexOf()
  • .slice()
  • .substring()
  • .substr()
  • .replace()
  • .trim()
  • .concat()

For example:

let str = "Hello, World!";
console.log(str.length); // Output: 13
console.log(str.toUpperCase()); // Output: "HELLO, WORLD!"

In this example, the built-in methods are used to manipulate the string without using the new keyword and the String constructor.

JS Basics