JavaScript Strings


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 Built-in String Functions

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:

  1. charAt()
  2. concat()
  3. indexOf()
  4. lastIndexOf()
  5. slice()
  6. split()
  7. toLowerCase()
  8. toUpperCase()
  9. trim()

1. charAt() Function

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"

2. concat() Function

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!"

3. indexOf() Function

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 

4. lastIndexOf() Function

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 

5. slice() Function

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"

6. split() Function

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!"]

7. toLowerCase() Function

This function is used to converts a string to lowercase.

const str = 'Hello, world!';
console.log(str.toLowerCase()); // Output: "hello, world!"

8. toUpperCase() Function

This function is used to converts a string to uppercase.

const str = 'Hello, world!';
console.log(str.toUpperCase()); // Output: "HELLO, WORLD!"

9. trim()

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.


Prev Next