JavaScript String

This blog post explores JavaScript strings. We’ll look at how to make them, change them, and use some common string methods. We’ll also show code examples.

1. Making a String

You can make strings with single quotes (‘) double quotes (“), or backticks (`).

Example:

<script>
let str1 = 'Hello';
let str2 = "World";
let str3 = `Hello, ${str2}!`;

console.log(str1); // Output: Hello
console.log(str2); // Output: World
console.log(str3); // Output: Hello World!
</script>

 

 

2. String Length

To find out how many characters a string has, you can use the length property.

Example:

<script>
let message = "JavaScript";
console.log(message.length); // Output: 10
</script>

 

3. Getting Characters

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

Example:

<script>
let greeting = "Hello";
console.log(greeting[1]); // Output: e
console.log(greeting.charAt(1)); // Output: e
</script>

 

 

4. Joining Strings

You can combine strings using the + operator or the concat() method.

Here’s an example:

<script>
let firstName = "John";
let lastName = "Doe";
let fullName = firstName + " " + lastName;
console.log(fullName); // Output: John Doe

let full = firstName.concat(" ", lastName);
console.log(full); // Output: John Doe
</script>

5. Changing Case

You can use toUpperCase() and toLowerCase() to alter the case of strings.

Here’s an example:

<script>
let text = "JavaScript";
console.log(text.toUpperCase()); // Output: JAVASCRIPT
console.log(text.toLowerCase()); // Output: javascript
</script>

 

 

6. Trimming Whitespace

To remove whitespace, you can apply trim() trimStart(), and trimEnd().

Check out this example:

<script>
let spaced = " Hello World ";
console.log(spaced.trim()); // Output: Hello World
</script>

 

7. Searching in a String

To look for substrings, you can use indexOf() lastIndexOf(), and includes().

Take a look at this example:

<script>
let phrase = "Learn JavaScript";
console.log(phrase.indexOf("Java")); // Output: 6
console.log(phrase.includes("Script")); // Output: true
</script>

 

 

8. Extracting Substrings

You can extract parts of a string using substring() slice(), or substr().

Here’s an example:

<script>
let example = "JavaScript";
console.log(example.substring(0 4)); // Output: Java
console.log(example.slice(4)); // Output: Script
</script>

 

 

9. Changing Text

You can use replace() or replaceAll() to swap out parts of a string.

Here’s an example:

<script>
let str = "I like cats";
let newStr = str.replace("cats", "dogs");
console.log(newStr); // Output: I like dogs
</script>

10. Breaking a String into Pieces

The split() method turns a string into an array.

Check out this example:

<script>
let data = "apple,banana cherry";
let items = data.split(",");
console.log(items); // Output: ["apple", "banana", "cherry"]
</script>

 

 

To wrap up

JavaScript strings are flexible and come with many built-in methods to handle and process text. Getting to grips with how to make and use these methods, you can handle a wide variety of tasks in web development efficiently.

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top