JS Strings

JS Strings

JS Strings:

In JavaScript, a string is a sequence of characters enclosed in quotation marks. Strings are used to represent text and can be manipulated in a variety of ways. Here’s an example of a simple string in JavaScript:

				
					let message = "Hello,world!";
				
			

You can access individual characters in a string using bracket notation:

				
					console.log(message[0]); //Output: "H"

console.log(message[7]); //Output: "w"
				
			

You can also concatenate strings using the + operator:

				
					let firstName ="John";

let lastName = "Doe";

let fullName = firstName +" " + lastName;

console.log(fullName); //Output: "John Doe"
				
			

There are many other methods and properties available for working with strings in JavaScript. Here are a few examples:

				
					let message = "Hello,world!";


console.log(message.length); //Output: 13


console.log(message.toUpperCase());// Output: "HELLO, WORLD!"


console.log(message.indexOf("world"));// Output: 7


console.log(message.substring(0,5)); // Output: "Hello"
				
			

The length property returns the number of characters in a string, while the toUpperCase() method returns a new string with all characters in uppercase. The indexOf() method returns the position of the first occurrence of a substring within a string, while the substring() method returns a substring of a string between two indices.

Understanding how to work with strings is an important part of writing effective JavaScript code.

String Methods:

JavaScript provides a variety of built-in methods for working with strings. Here are some common string methods:

1.    length: Returns the length of a string.

				
					let message = "Hello,world!";

console.log(message.length); //Output: 13
				
			

2.    toUpperCase(): Returns a new string with all characters converted to uppercase.

				
					let message = "Hello,world!";

console.log(message.toUpperCase());// Output: "HELLO, WORLD!"
				
			

3.    toLowerCase(): Returns a new string with all characters converted to lowercase.

				
					let message = "Hello,world!";

console.log(message.toLowerCase());// Output: "hello, world!"
				
			

4.    indexOf(): Returns the position of the first occurrence of a substring within a string. Returns -1 if the substring is not found.

				
					let message = "Hello,world!";

console.log(message.indexOf("world"));// Output: 7

console.log(message.indexOf("JavaScript"));// Output: -1
				
			

5.    substring(): Returns a substring of a string between two indices.

				
					let message = "Hello,world!";

console.log(message.substring(0,5)); // Output: "Hello"
				
			

6.    slice(): Returns a portion of a string between two indices. Works similarly to substring(), but also allows negative indices (which count from the end of the string).

				
					let message = "Hello,world!";

console.log(message.slice(7));// Output: "world!"

console.log(message.slice(-6));// Output: "world!"
				
			

7.    replace(): Returns a new string with all occurrences of a substring replaced with another string.

				
					let message = "Hello,world!";

console.log(message.replace("world","JavaScript")); // Output: "Hello,JavaScript!"
				
			

8.    split(): Returns an array of substrings split from a string by a delimiter.

				
					let message = "Hello,world!";

console.log(message.split("")); // Output: ["Hello,", "world!"]
				
			

These are just a few examples of the many string methods available in JavaScript. Understanding how to use these methods can help you work more effectively with text in your JavaScript code.

String Search:

In JavaScript, you can search for substrings within a string using the indexOf() and lastIndexOf() methods.

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

				
					let str = "Hello,world!";

console.log(str.indexOf("world"));// Output: 7

console.log(str.indexOf("JavaScript"));// Output: -1
				
			

The lastIndexOf() method is similar to indexOf(), but returns the index of the last occurrence of a substring within a string.

				
					let str = "Hello,world!";

console.log(str.lastIndexOf("o"));// Output: 8

console.log(str.lastIndexOf("JavaScript"));// Output: -1
				
			

Both methods also allow you to specify a starting index for the search:

				
					let str = "Hello,world!";

console.log(str.indexOf("o",5)); // Output: 8

console.log(str.lastIndexOf("o",5)); // Output: 4
				
			

In addition to these methods, JavaScript also provides the search() method, which searches for a regular expression within a string. The search() method returns the index of the first match, or -1 if no match is found.

				
					let str = "Hello,world!";

console.log(str.search(/world/));// Output: 7

console.log(str.search(/JavaScript/));// Output: -1
				
			

These methods can be useful for searching through strings in a variety of contexts, such as parsing user input or manipulating text data.

String Template:

String templates (or template literals) are a feature in JavaScript that allow you to embed expressions and variables directly within a string. This can make your code more concise and easier to read, especially when you need to combine several values into a single string.

To create a string template, you enclose the string in backticks (“) instead of single or double quotes:

				
					let name = "Alice";

let age = 30;

let message = `My name is${name} and I am ${age} years old.`;

console.log(message); // Output:"My name is Alice and I am 30 yearsold."
				
			

Within the string template, you can include expressions and variables using the ${} syntax. These expressions will be evaluated and the resulting values will be inserted into the string.

				
					let x = 10;

let y = 20;

let result = `The sum of ${x}and ${y} is ${x + y}.`;

console.log(result); // Output:"The sum of 10 and 20 is 30."
				
			

You can also include any valid JavaScript expression within the ${} syntax. For example:

				
					let x = 10;

let result = `The square of ${x}is ${x * x}.`;

console.log(result); // Output:"The square of 10 is 100." 
				
			

String templates can be a powerful tool for building complex strings in your
JavaScript code, and can make your code easier to read and maintain.

Join To Get Our Newsletter
Spread the love