JavaScript Strings & Methods Guide
JavaScript strings and methods

JavaScript Strings & Methods Guide

String Introduction

In JavaScript, we store text data in variables so that text data is called strings.

//Examples
//Wrong way to write strings
let name = Waleed;

//Right way to write strings
let name = "Waleed"
or 
let name = 'Waleed'
or
const greeting = `Hello, ${name}! 
How are you today?`;        

We can use ("" , '' , ``) these to write string. Strings are primitive data types which is used to store text data.

String Most Used Methods

1. toUpperCase()

This method is used to make strings into uppercase.

let car = "honda"
console.log(car.toUpperCase());
//Answer is : HONDA        

2. toLowerCase()

This method is used to make strings into lowercase

let car = "HONDA"
console.log(car.toLowerCase());
//Answer is : honda        

3. concat( )

The concat() method is used to join two strings (str1 and " ", and str2) together, with a space in between.

const str1 = "Hello";
const str2 = "World";

const result = str1.concat(" ", str2);

console.log(result); // Output: "Hello World"        

4. slice( )

The slice() method in JavaScript is used to extract a section of a string and return a new string without modifying the original string. It takes two parameters: the starting index and the ending index of the slice.

const str = "Hello, world!";
const sliced = str.slice(7, 12); // Extracts characters from index 7 to 11
console.log(sliced); // Output: "world"        

5. substring( )

The substring() method in JavaScript is used to extract a portion of a string and return it as a new string. It takes two parameters: the starting index and the ending index of the substring.

const str = "Hello, world!";
const substring = str.substring(7, 12); // Extract substring starting from index 7 to index 12

console.log(substring); // Output: "world"        

6. replace( )

The replace() method in JavaScript is used to replace occurrences of a specified substring

const str = "Hello, world!";
const newStr = str.replace("world", "Universe");
console.log(newStr); // Output: Hello, Universe!        

7. includes( )

This code checks if the string "Hello, world!" includes the substring "world". The includes() method returns true because "world" is present within the string.

let str = "Hello, world"
console.log(str.includes("world")); // Output: true        

8. indexOf( )

The indexOf() method in JavaScript returns the index of the first occurrence of a specified substring within a string. If the substring is not found, it returns -1.

const str = "Hello, world!";
const index = str.indexOf("world");
console.log(index); // Output: 7        

9. charAt( )

The charAt() method in JavaScript returns the character at a specified index in a string.

const str = "Hello";
const charAtIndex2 = str.charAt(2);
console.log(charAtIndex2); // Output: "l"        

10. split( )

The split() method in JavaScript is used to split a string into an array of substrings based on a specified separator.

const str = "apple,banana,orange";
const fruitsArray = str.split(",");

console.log(fruitsArray);
["apple", "banana", "orange"]        

11. trim( )

The trim() method in JavaScript removes whitespace from both ends of a string.

const str = "  Hello, world!   ";
const trimmedStr = str.trim();

console.log(trimmedStr); // Output: "Hello, world!"        

12. endWith( )

The endsWith() method in JavaScript is used to determine whether a string ends with the characters of a specified string, returning true or false as appropriate.

const str = "Hello, world!";
const endsWithExclamation = str.endsWith("!");

console.log(endsWithExclamation); // Output: true        

13. startsWith( )

The startsWith() method in JavaScript is used to determine whether a string begins with the characters of a specified string.

const str = "Hello, world!";
const startsWithHello = str.startsWith("Hello");

console.log(startsWithHello); // Output: true        

Conclusion:

Strings are fundamental data types in JavaScript, widely used for representing text. Understanding the various string methods allows developers to manipulate and work with strings effectively, enabling tasks such as string modification, searching, and validation. By leveraging these methods, developers can enhance the functionality and usability of their JavaScript applications.

To view or add a comment, sign in

More articles by Muhammad Waleed

Explore content categories