In JavaScript, a string is a sequence of characters enclosed in single or double quotes. Strings are a primitive data type in JavaScript, which means that they are not objects and cannot have properties or methods.
JavaScript provides several built-in string functions that allow you to manipulate and work with strings. Here are some commonly used string functions in JavaScript:
This function is used to returns the character at a specified index in a string.
const str = 'Hello, world!';
console.log(str.charAt(0)); // Output: "H"
This function is used to combines two or more strings and returns a new string.
const str1 = 'Hello';
const str2 = 'World';
console.log(str1.concat(', ', str2, '!')); // Output: "Hello, World!"
This function is used to returns the index of the first occurrence of a specified string or character in a string.
const str = 'Hello, world!';
console.log(str.indexOf('o')); // Output: 4
This function is used to returns the index of the last occurrence of a specified string or character in a string.
const str = 'Hello, world!';
console.log(str.lastIndexOf('o')); // Output: 8
This function is used to extracts a section of a string and returns a new string.
const str = 'Hello, world!';
console.log(str.slice(0, 5)); // Output: "Hello"
This function is used to splits a string into an array of substrings based on a specified separator.
const str = 'Hello, world!';
console.log(str.split(', ')); // Output: ["Hello", "world!"]
This function is used to converts a string to lowercase.
const str = 'Hello, world!';
console.log(str.toLowerCase()); // Output: "hello, world!"
This function is used to converts a string to uppercase.
const str = 'Hello, world!';
console.log(str.toUpperCase()); // Output: "HELLO, WORLD!"
This function is used to removes whitespace from the beginning and end of a string.
const str = ' Hello, world! ';
console.log(str.trim()); // Output: "Hello, world!"
These are all few examples of the many string functions available in JavaScript. By using these functions, you can manipulate and work with strings in a variety of ways.
Here are a few examples of how to create and manipulate strings in JavaScript:
const str1 = 'Hello, world!'; // Using single quotes
const str2 = "Hello, world!"; // Using double quotes
const str3 = `Hello, world!`; // Using backticks (template literals)
console.log(str1); // Output: "Hello, world!"
console.log(str2); // Output: "Hello, world!"
console.log(str3); // Output: "Hello, world!"
// Concatenation
const firstName = 'Rohatash';
const lastName = 'Kumar';
const fullName = firstName + ' ' + lastName;
console.log(fullName); // Output: "Rohatash Kumar"
// String methods
const str4 = ' Hello, world! ';
console.log(str4.trim()); // Output: "Hello, world!"
console.log(str4.toUpperCase()); // Output: " HELLO, WORLD! "
console.log(str4.toLowerCase()); // Output: " hello, world! "
console.log(str4.length); // Output: 19
console.log(str4.charAt(0)); // Output: " "
In this example, we create strings using single quotes, double quotes, and backticks. We also concatenate strings using the + operator and use various string methods, such as trim(), toUpperCase(), toLowerCase(), length, and charAt().
JavaScript strings are immutable, which means that once a string is created, it cannot be modified. However, you can create new strings by concatenating, slicing, or replacing parts of existing strings.