js String

A JavaScript string is an object that represents a sequence of characters. Strings are used to store and manipulate text in JavaScript. You can create strings in two ways: by using string literals or by creating a string object using the new keyword.

Creating Strings in JavaScript

1. By String Literal

The most common way to create a string is by using string literals. A string literal is a sequence of characters enclosed in single or double quotes.

Example:

<script >
var greeting = "Hello!!";
var name = 'way to code.';

document.write(greeting + " ");
document.write(name);
</script >

Output:

Hello!! way to code.

2. By String Object (Using new Keyword)

You can also create a string by creating an instance of the String object using the new keyword. This method is less common but still useful.

Example:

<script>
var message = new String("Hello,way to code.");

document.write(message+"
");
document.write("Length of message: " + message.length + "
");
</script>

Output:

Hello,way to code.
Length of message: 18

String Properties and Methods

JavaScript strings come with several useful properties and methods that allow you to manipulate and interact with them. Here are a few examples

1. length Property

The length property returns the number of characters in a string.

Example:

<script>
var text = "JavaScript String";

document.write( text + "
");
document.write("Length of text: " + text.length + "
");
</script>

Output:

JavaScript String
Length of text: 17

2. toUpperCase() Method

The toUpperCase() method converts a string to uppercase letters.

Example:

<script>
var text = "JavaScript String";
var upperText = text.toUpperCase();

document.write("Original text: " + text + "
");
document.write("Uppercase text: " + upperText + "
");
</script>

Output:

Original text: JavaScript String
Uppercase text: JAVASCRIPT STRING

3. toLowerCase() Method

The toLowerCase() method converts a string to lowercase letters.

Example:

<script>
var text = "JavaScript String";
var lowerText = text.toLowerCase();

document.write("Original text: " + text + "
");
document.write("Lowercase text: " + lowerText + "
");
</script>

Output:

Original text: JavaScript String
Lowercase text: javascript string

4. charAt() Method

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

Example:

<script>
var text = "JavaScript String";
var charAtFive = text.charAt(5);

document.write("Character at index 5: " + charAtFive + "
");
</script>

Output:

Character at index 5: c

Let's see the list of JavaScript string methods with examples.

Method Description Example
charAt() Returns the character at the specified index. var str = "Hello"; document.write(str.charAt(1)); // Output: "e"
charCodeAt() Returns the Unicode of the character at the specified index. var str = "Hello"; document.write(str.charCodeAt(1)); // Output: 101
concat() Combines two or more strings. var str1 = "Hello"; var str2 = "World"; document.write(str1.concat(" ", str2)); // Output: "Hello World"
indexOf() Returns the index of the first occurrence of a specified value in a string. var str = "Hello World"; document.write(str.indexOf("World")); // Output: 6
lastIndexOf() Returns the index of the last occurrence of a specified value in a string. var str = "Hello World"; document.write(str.lastIndexOf("o")); // Output: 7
search() Searches for a match between a regular expression and a string. var str = "Hello World"; document.write(str.search("World")); // Output: 6
match() Returns an array containing all matches of a regular expression in a string. var str = "Hello World"; document.write(str.match(/o/g)); // Output: o,o
replace() Replaces a specified value with another value in a string. var str = "Hello World"; document.write(str.replace("World", "Everyone")); // Output: "Hello Everyone"
substr() Extracts a part of a string, beginning at the specified start position, and through the specified number of characters. var str = "Hello World"; document.write(str.substr(1, 4)); // Output: "ello"
substring() Extracts characters from a string between two specified indices. var str = "Hello World"; document.write(str.substring(1, 4)); // Output: "ell"
slice() Extracts a part of a string and returns the extracted part in a new string. var str = "Hello World"; document.write(str.slice(1, 4)); // Output: "ell"
toLowerCase() Converts a string to lowercase letters. var str = "Hello World"; document.write(str.toLowerCase()); // Output: "hello world"
toLocaleLowerCase() Converts a string to lowercase letters, according to the host's locale. var str = "Hello World"; document.write(str.toLocaleLowerCase()); // Output: "hello world"
toUpperCase() Converts a string to uppercase letters. var str = "Hello World"; document.write(str.toUpperCase()); // Output: "HELLO WORLD"
toLocaleUpperCase() Converts a string to uppercase letters, according to the host's locale. var str = "Hello World"; document.write(str.toLocaleUpperCase()); // Output: "HELLO WORLD"
toString() Returns a string representing the specified object. var str = "Hello World"; document.write(str.toString()); // Output: "Hello World"
valueOf() Returns the primitive value of a string object. var str = new String("Hello World"); document.write(str.valueOf()); // Output: "Hello World"
split() Splits a string into an array of substrings, and returns the new array. var str = "Hello World"; document.write(str.split(" ")); // Output: Hello,World
trim() Removes whitespace from both ends of a string. var str = " Hello World "; document.write(str.trim()); // Output: "Hello World"