JavaScript(JS) Strings

‮tth‬ps://www.theitroad.com

JavaScript Strings

In JavaScript, a string is a sequence of characters enclosed in single or double quotes. Strings can be created using string literals or by calling the String() constructor.

Here are some examples of creating strings:

let myString = "Hello, World!";
let anotherString = 'JavaScript is fun!';
let thirdString = new String("This is a string created with the String() constructor.");

You can access individual characters in a string using bracket notation, just like with arrays:

let myString = "Hello, World!";
console.log(myString[0]); // Output: "H"
console.log(myString[7]); // Output: "W"

Strings have many built-in methods that allow you to manipulate them. Here are some commonly used string methods:

  1. length: Returns the length of the string.
let myString = "Hello, World!";
console.log(myString.length); // Output: 13
  1. indexOf(): Returns the index of the first occurrence of a specified substring in the string, or -1 if the substring is not found.
let myString = "Hello, World!";
console.log(myString.indexOf("World")); // Output: 7
console.log(myString.indexOf("JavaScript")); // Output: -1
  1. slice(): Extracts a section of the string and returns a new string.
let myString = "Hello, World!";
console.log(myString.slice(0, 5)); // Output: "Hello"
console.log(myString.slice(7)); // Output: "World!"
  1. toUpperCase(): Converts the string to uppercase.
let myString = "Hello, World!";
console.log(myString.toUpperCase()); // Output: "HELLO, WORLD!"
  1. toLowerCase(): Converts the string to lowercase.
let myString = "Hello, World!";
console.log(myString.toLowerCase()); // Output: "hello, world!"
  1. replace(): Replaces a specified substring with another substring in the string.
let myString = "Hello, World!";
console.log(myString.replace("World", "JavaScript")); // Output: "Hello, JavaScript!"

These are just a few of the many string methods available in JavaScript. You can check out the full list of string methods in the official documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String

Access String Characters

You can access individual characters in a string in JavaScript using bracket notation or charAt() method.

Using bracket notation:

let myString = "Hello, World!";
console.log(myString[0]); // Output: "H"
console.log(myString[7]); // Output: "W"

Using the charAt() method:

let myString = "Hello, World!";
console.log(myString.charAt(0)); // Output: "H"
console.log(myString.charAt(7)); // Output: "W"

Both of these methods return the character at the specified index in the string. The index is a zero-based integer, meaning that the first character in the string has an index of 0, the second character has an index of 1, and so on.

If you try to access an index that is outside the range of the string, both methods will return an empty string:

let myString = "Hello, World!";
console.log(myString[20]); // Output: undefined
console.log(myString.charAt(20)); // Output: ""

It's important to note that strings in JavaScript are immutable, which means that you cannot change the value of an individual character in a string. However, you can create a new string with the desired changes using methods like slice() or replace().

JavaScript Strings are immutable

In JavaScript, strings are immutable, which means that once a string is created, its value cannot be changed. You cannot change the value of an individual character in a string directly.

For example, if you have a string "hello", you cannot change the value of the "o" character to "p" to make the string "help".

let str = "hello";
str[4] = "p"; // This has no effect
console.log(str); // Output: "hello"

Instead, you need to create a new string with the desired changes. Here are a few ways to achieve this:

  1. Using the slice() method:
let str = "hello";
let newStr = str.slice(0, 4) + "p";
console.log(newStr); // Output: "help"
  1. Using the replace() method:
let str = "hello";
let newStr = str.replace("o", "p");
console.log(newStr); // Output: "help"
  1. Using template literals:
let str = "hello";
let newStr = `${str.slice(0, 4)}p`;
console.log(newStr); // Output: "help"

It's important to keep in mind that each of these methods creates a new string and does not modify the original string.

JavaScript is Case-Sensitive

JavaScript is case-sensitive, which means that it distinguishes between uppercase and lowercase letters. For example, the variable names "myVar" and "myvar" are considered to be two different variables in JavaScript.

Here's an example to illustrate this:

let myVar = "hello";
let myvar = "world";

console.log(myVar); // Output: "hello"
console.log(myvar); // Output: "world"

In this example, we have two variables named "myVar" and "myvar". Even though they differ only in the capitalization of the "V" letter, they are considered to be two different variables in JavaScript.

It's important to pay attention to the case of the letters when working with JavaScript code, especially when writing variable names, function names, and object properties. If you use the wrong case, JavaScript may not be able to find the variable or function you are trying to access, which can lead to errors in your code.

JavaScript Multiline Strings

In JavaScript, you can create multiline strings using template literals or by concatenating multiple strings.

Using Template Literals:

Template literals are enclosed by backticks (``) and can span multiple lines. You can use placeholders (${expression}) to embed JavaScript expressions inside the string.

Here's an example of creating a multiline string using template literals:

let myString = `This is a
multiline
string`;

console.log(myString);
// Output:
// This is a
// multiline
// string

Using String Concatenation:

You can also create a multiline string by concatenating multiple strings together using the + operator. To break a string into multiple lines, you can use the newline character (\n).

Here's an example of creating a multiline string using string concatenation:

let myString = "This is a " +
               "multiline " +
               "string";

console.log(myString);
// Output: This is a multiline string

Both of these methods will create a multiline string, but using template literals is generally considered to be more concise and readable, especially if you need to embed JavaScript expressions inside the string.